Skip to content

Commit

Permalink
ui.FileSelector logic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 12, 2022
1 parent 18e2d40 commit 6837089
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 15 deletions.
51 changes: 42 additions & 9 deletions internal/app/ui/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ import (
)

type fileSelectorOpt struct {
emptyFilename string // if set, the empty filename will be replaced to this value
defaultFilename string // if set, the empty filename will be replaced to this value
mustExist bool
}

func WithEmptyFilename(s string) Option {
return func(so *inputOptions) {
so.fileSelectorOpt.emptyFilename = s
func WithDefaultFilename(s string) Option {
return func(io *inputOptions) {
io.fileSelectorOpt.defaultFilename = s
}
}

func WithMustExist(b bool) Option {
return func(io *inputOptions) {
io.mustExist = b
}
}

Expand All @@ -34,10 +41,22 @@ func FileSelector(msg, descr string, opt ...Option) (string, error) {
Help: descr,
},
Validate: func(ans interface{}) error {
if ans.(string) != "" || opts.emptyFilename != "" {
return nil
filename := ans.(string)
if filename == "" {
if opts.defaultFilename == "" {
return errors.New("empty filename")
} else {
if !opts.mustExist {
return nil
} else {
return checkExists(opts.defaultFilename)
}
}
}
return errors.New("empty filename")
if opts.mustExist {
return checkExists(filename)
}
return nil
},
},
}
Expand All @@ -49,8 +68,11 @@ func FileSelector(msg, descr string, opt ...Option) (string, error) {
if err := survey.Ask(q, &resp, opts.surveyOpts()...); err != nil {
return "", err
}
if resp.Filename == "" && opts.emptyFilename != "" {
resp.Filename = opts.emptyFilename
if resp.Filename == "" && opts.defaultFilename != "" {
resp.Filename = opts.defaultFilename
}
if opts.mustExist {
break
}
if _, err := os.Stat(resp.Filename); err != nil {
break
Expand All @@ -65,3 +87,14 @@ func FileSelector(msg, descr string, opt ...Option) (string, error) {
}
return resp.Filename, nil
}

func checkExists(filename string) error {
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
return errors.New("file must exist")
} else {
return err
}
}
return nil
}
60 changes: 54 additions & 6 deletions internal/app/ui/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {
}

func TestFileselector(t *testing.T) {
t.Run("default", func(t *testing.T) {
t.Run("filename set", func(t *testing.T) {
var filename string
testFn := func(stdio terminal.Stdio) error {
var err error
Expand All @@ -32,7 +32,7 @@ func TestFileselector(t *testing.T) {
RunTest(t, procedure, testFn)
assert.Equal(t, "test.txt", filename)
})
t.Run("empty with no override", func(t *testing.T) {
t.Run("empty, default not set", func(t *testing.T) {
var filename string
testFn := func(stdio terminal.Stdio) error {
var err error
Expand All @@ -50,11 +50,11 @@ func TestFileselector(t *testing.T) {
RunTest(t, procedure, testFn)
assert.Equal(t, ":wq!", filename)
})
t.Run("empty with the override", func(t *testing.T) {
t.Run("empty, default set", func(t *testing.T) {
var filename string
testFn := func(stdio terminal.Stdio) error {
var err error
filename, err = FileSelector("xxx", "help", WithOutput(stdio), WithEmptyFilename("override"))
filename, err = FileSelector("xxx", "help", WithOutput(stdio), WithDefaultFilename("default_filename"))
return err
}
procedure := func(t *testing.T, c console) {
Expand All @@ -63,9 +63,9 @@ func TestFileselector(t *testing.T) {
c.ExpectEOF()
}
RunTest(t, procedure, testFn)
assert.Equal(t, "override", filename)
assert.Equal(t, "default_filename", filename)
})
t.Run("overwrite", func(t *testing.T) {
t.Run("filename set and exist", func(t *testing.T) {
var filename string
dir := t.TempDir()
testfile := filepath.Join(dir, "testfile.txt")
Expand All @@ -88,4 +88,52 @@ func TestFileselector(t *testing.T) {
})
assert.Equal(t, testfile, filename)
})
t.Run("filename set, default empty, must exist set", func(t *testing.T) {
var filename string
dir := t.TempDir()
testfile := filepath.Join(dir, "testfile.txt")
if err := os.WriteFile(testfile, []byte("unittest"), 0666); err != nil {
t.Fatal(err)
}
RunTest(
t,
func(t *testing.T, c console) {
// attempt 1.
c.ExpectString("non-existing")
c.SendLine(filepath.Join(dir, "$$$$.XXX"))
c.ExpectString("file must exist")
// attempt 2.
c.ExpectString("non-existing")
c.SendLine(testfile)
c.ExpectEOF()
},
func(s terminal.Stdio) error {
var err error
filename, err = FileSelector("non-existing test", "", WithOutput(s), WithMustExist(true))
return err
})
assert.Equal(t, testfile, filename)
})
t.Run("filename not set, default set, must exist set", func(t *testing.T) {
var filename string
dir := t.TempDir()
testfile := filepath.Join(dir, "testfile.txt")
if err := os.WriteFile(testfile, []byte("unittest"), 0666); err != nil {
t.Fatal(err)
}
RunTest(
t,
func(t *testing.T, c console) {
// attempt 1.
c.ExpectString("variant 4")
c.SendLine("")
c.ExpectEOF()
},
func(s terminal.Stdio) error {
var err error
filename, err = FileSelector("variant 4", "", WithOutput(s), WithDefaultFilename(testfile), WithMustExist(true))
return err
})
assert.Equal(t, testfile, filename)
})
}

0 comments on commit 6837089

Please sign in to comment.