Skip to content

Commit

Permalink
Go: Fixed an off-by-one bug in the mock questioner AskChoice (#6572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laren-AWS authored Jun 21, 2024
1 parent f60de44 commit e26d2c3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gov2/demotools/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (filesystem FileSystem) OpenFile(filename string) (io.ReadWriteCloser, erro

// CloseFile closes the provided file.
func (filesystem FileSystem) CloseFile(file io.ReadWriteCloser) {
defer file.Close()
file.Close()
}

// MockFileSystem is a mock version of IFileSystem for testing.
Expand All @@ -69,5 +69,5 @@ func (filesystem MockFileSystem) OpenFile(_ string) (io.ReadWriteCloser, error)

// CloseFile closes the io.ReadWriteCloser provided on object creation.
func (filesystem MockFileSystem) CloseFile(_ io.ReadWriteCloser) {
defer filesystem.mockfile.Close()
filesystem.mockfile.Close()
}
8 changes: 5 additions & 3 deletions gov2/demotools/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ func TestNewStandardFileSystemGwd(t *testing.T) {
func TestNewStandardFileSystemFileIO(t *testing.T) {
filesystem := NewStandardFileSystem()
filename := "test.txt"
os.Create(filename)
file, err := filesystem.OpenFile(filename)
file, err := os.Create(filename)
fsFile, err := filesystem.OpenFile(filename)
if err != nil {
t.Errorf("NewStandardFileSystemFileInteraction(): error opening file: %v", err)
}
filesystem.CloseFile(file)
filesystem.CloseFile(fsFile)
file.Close()
err = os.Remove(filename)
if err != nil {
t.Errorf("NewStandardFileSystemFileInteraction(): error removing file: %v", err)
Expand All @@ -48,6 +49,7 @@ func TestNewMockFileSystem(t *testing.T) {
t.Errorf("NewMockFileSystem(): error opening file: %v", fErr)
}
filesystem.CloseFile(mockFile)
file.Close()
err = os.Remove(filename)
if err != nil {
t.Errorf("NewMockFileSystem(): error removing file: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion gov2/demotools/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (mock *MockQuestioner) AskFloat64(question string, validators ...IAnswerVal

func (mock *MockQuestioner) AskChoice(question string, choices []string) int {
answerInt, _ := strconv.Atoi(mock.Next(question))
return answerInt
return answerInt - 1
}

func (mock *MockQuestioner) AskPassword(question string, minLength int) string {
Expand Down

0 comments on commit e26d2c3

Please sign in to comment.