Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
catatsuy committed Aug 28, 2021
1 parent dc2b0aa commit 37b69fb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (c *CLI) run(target string, debug bool, bufSize int64) int {

bb := (*[256]byte)(unsafe.Pointer(&dirent.Name[0]))
name := string(bb[0:blen(*bb)])

if name == "." || name == ".." {
// ignore
continue
}
fmt.Fprintln(c.outStream, name)
}

Expand Down
45 changes: 45 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cli

import (
"bytes"
"os"
"sort"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestRun_Success(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cl := NewCLI(outStream, errStream)
target := "testdata"
status := cl.run(target, false, 0)
if status != ExitCodeOK {
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeOK)
}
actualList := strings.Split(outStream.String(), "\n")
// last element is unused
actualList = actualList[:len(actualList)-1]
expectedList := dirList(t, target)
sort.Slice(actualList, func(i, j int) bool { return actualList[i] < actualList[j] })

if diff := cmp.Diff(actualList, expectedList); diff != "" {
t.Errorf("file list mismatch (-actual +expected):\n%s", diff)
}
}

func dirList(t *testing.T, target string) []string {
files, err := os.ReadDir(target)
if err != nil {
t.Error(err)
}

names := make([]string, 0, len(files))

for _, file := range files {
names = append(names, file.Name())
}

return names
}
Empty file added cli/testdata/abcd
Empty file.
Empty file added cli/testdata/bbb
Empty file.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/catatsuy/lls

go 1.17

require github.com/google/go-cmp v0.5.6
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit 37b69fb

Please sign in to comment.