Skip to content

Commit

Permalink
fix(test): Handle platform-specific "file not found" messages
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Dec 10, 2023
1 parent 404418a commit ea972dd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/alecthomas/kong

require (
github.com/alecthomas/assert/v2 v2.1.0
github.com/alecthomas/repr v0.1.0
github.com/alecthomas/assert/v2 v2.4.1
github.com/alecthomas/repr v0.3.0
)

require github.com/hexops/gotextdiff v1.0.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/alecthomas/assert/v2 v2.4.1 h1:mwPZod/d35nlaCppr6sFP0rbCL05WH9fIo7lvsf47zo=
github.com/alecthomas/assert/v2 v2.4.1/go.mod h1:fw5suVxB+wfYJ3291t0hRTqtGzFYdSwstnRQdaQx2DM=
github.com/alecthomas/repr v0.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8=
github.com/alecthomas/repr v0.3.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
41 changes: 23 additions & 18 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"math"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -460,15 +460,13 @@ func TestFileMapper(t *testing.T) {
_ = cli.File.Close()
_, err = p.Parse([]string{"testdata/missing.txt"})
assert.Error(t, err)
if runtime.GOOS == "windows" {
assert.Contains(t, err.Error(), "missing.txt: The system cannot find the file specified.")
} else {
assert.Contains(t, err.Error(), "missing.txt: no such file or directory")
}
assert.Contains(t, err.Error(), "missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
_, err = p.Parse([]string{"-"})
assert.NoError(t, err)
assert.Equal(t, os.Stdin, cli.File)
}

func TestFileContentMapper(t *testing.T) {
type CLI struct {
File []byte `type:"filecontent"`
Expand All @@ -481,9 +479,11 @@ func TestFileContentMapper(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--file", "testdata/missing.txt"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing.txt: no such file or directory")
assert.Contains(t, err.Error(), "missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--file", "testdata/"})

_, err = p.Parse([]string{"--file", "testdata"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "is a directory")
}
Expand All @@ -501,7 +501,8 @@ func TestExistingFileMapper(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--file", "testdata/missing.txt"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing.txt: no such file or directory")
assert.Contains(t, err.Error(), "missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--file", "testdata/"})
assert.Error(t, err)
Expand All @@ -514,15 +515,16 @@ func TestExistingFileMapperDefaultMissing(t *testing.T) {
}
var cli CLI
p := mustNew(t, &cli)
file := "testdata/file.txt"
file := filepath.Join("testdata", "file.txt")
_, err := p.Parse([]string{"--file", file})
assert.NoError(t, err)
assert.NotZero(t, cli.File)
assert.Contains(t, cli.File, file)
p = mustNew(t, &cli)
_, err = p.Parse([]string{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing.txt: no such file or directory")
assert.Contains(t, err.Error(), "missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
}

func TestExistingFileMapperDefaultMissingCmds(t *testing.T) {
Expand All @@ -536,7 +538,7 @@ func TestExistingFileMapperDefaultMissingCmds(t *testing.T) {
} `cmd:""`
}
var cli CLI
file := "testdata/file.txt"
file := filepath.Join("testdata", "file.txt")
p := mustNew(t, &cli)
_, err := p.Parse([]string{"cmd-a", "--file-a", file, "--file-b", file})
assert.NoError(t, err)
Expand All @@ -547,7 +549,8 @@ func TestExistingFileMapperDefaultMissingCmds(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{"cmd-a", "--file-a", file})
assert.Error(t, err)
assert.Contains(t, err.Error(), "bbb-missing.txt: no such file or directory")
assert.Contains(t, err.Error(), "bbb-missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
}

//nolint:dupl
Expand All @@ -563,7 +566,8 @@ func TestExistingDirMapper(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--dir", "missingdata/"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missingdata: no such file or directory")
assert.Contains(t, err.Error(), "missingdata:")
assert.IsError(t, err, os.ErrNotExist)
p = mustNew(t, &cli)
_, err = p.Parse([]string{"--dir", "testdata/file.txt"})
assert.Error(t, err)
Expand All @@ -584,7 +588,8 @@ func TestExistingDirMapperDefaultMissing(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing-dir: no such file or directory")
assert.Contains(t, err.Error(), "missing-dir:")
assert.IsError(t, err, os.ErrNotExist)
}

func TestExistingDirMapperDefaultMissingCmds(t *testing.T) {
Expand All @@ -609,7 +614,8 @@ func TestExistingDirMapperDefaultMissingCmds(t *testing.T) {
p = mustNew(t, &cli)
_, err = p.Parse([]string{"cmd-a", "--dir-a", dir})
assert.Error(t, err)
assert.Contains(t, err.Error(), "bbb-missing-dir: no such file or directory")
assert.Contains(t, err.Error(), "bbb-missing-dir:")
assert.IsError(t, err, os.ErrNotExist)
}

func TestMapperPlaceHolder(t *testing.T) {
Expand All @@ -632,8 +638,7 @@ func TestMapperPlaceHolder(t *testing.T) {
assert.Contains(t, b.String(), "--flag=/a/b/c")
}

type testMapperWithPlaceHolder struct {
}
type testMapperWithPlaceHolder struct{}

func (t testMapperWithPlaceHolder) Decode(ctx *kong.DecodeContext, target reflect.Value) error {
target.SetString("hi")
Expand Down
3 changes: 2 additions & 1 deletion mapper_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestWindowsFileMapper(t *testing.T) {
_ = cli.File.Close()
_, err = p.Parse([]string{"testdata\\missing.txt"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing.txt: The system cannot find the file specified.")
assert.Contains(t, err.Error(), "missing.txt:")
assert.IsError(t, err, os.ErrNotExist)
_, err = p.Parse([]string{"-"})
assert.NoError(t, err)
assert.Equal(t, os.Stdin, cli.File)
Expand Down

0 comments on commit ea972dd

Please sign in to comment.