Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Fixed an off-by-one bug in the mock questioner AskChoice #6572

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading