Skip to content

Commit 774c657

Browse files
committed
Fixed an off-by-one bug in the mock questioner AskChoice, fixed FS bugs so files are closed properly.
1 parent 67f70d5 commit 774c657

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

gov2/demotools/filesystem.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (filesystem FileSystem) OpenFile(filename string) (io.ReadWriteCloser, erro
4242

4343
// CloseFile closes the provided file.
4444
func (filesystem FileSystem) CloseFile(file io.ReadWriteCloser) {
45-
defer file.Close()
45+
file.Close()
4646
}
4747

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

7070
// CloseFile closes the io.ReadWriteCloser provided on object creation.
7171
func (filesystem MockFileSystem) CloseFile(_ io.ReadWriteCloser) {
72-
defer filesystem.mockfile.Close()
72+
filesystem.mockfile.Close()
7373
}

gov2/demotools/filesystem_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ func TestNewStandardFileSystemGwd(t *testing.T) {
2020
func TestNewStandardFileSystemFileIO(t *testing.T) {
2121
filesystem := NewStandardFileSystem()
2222
filename := "test.txt"
23-
os.Create(filename)
24-
file, err := filesystem.OpenFile(filename)
23+
file, err := os.Create(filename)
24+
fsFile, err := filesystem.OpenFile(filename)
2525
if err != nil {
2626
t.Errorf("NewStandardFileSystemFileInteraction(): error opening file: %v", err)
2727
}
28-
filesystem.CloseFile(file)
28+
filesystem.CloseFile(fsFile)
29+
file.Close()
2930
err = os.Remove(filename)
3031
if err != nil {
3132
t.Errorf("NewStandardFileSystemFileInteraction(): error removing file: %v", err)
@@ -48,6 +49,7 @@ func TestNewMockFileSystem(t *testing.T) {
4849
t.Errorf("NewMockFileSystem(): error opening file: %v", fErr)
4950
}
5051
filesystem.CloseFile(mockFile)
52+
file.Close()
5153
err = os.Remove(filename)
5254
if err != nil {
5355
t.Errorf("NewMockFileSystem(): error removing file: %v", err)

gov2/demotools/mocks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (mock *MockQuestioner) AskFloat64(question string, validators ...IAnswerVal
4747

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

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

0 commit comments

Comments
 (0)