File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,24 @@ func NewRoodCmd() *cobra.Command {
20
20
dir = args [0 ]
21
21
}
22
22
23
+ // Stat the file so we can check if it's a directory or not before
24
+ // we try to read it as a directory using ReadDir. os.ReadDir will
25
+ // generates an error if the thing you pass to it is not a directory.
26
+ // https://pkg.go.dev/os#Stat
27
+ fileInfo , err := os .Stat (dir )
28
+ if err != nil {
29
+ return err
30
+ }
31
+
32
+ // We can only list the contents of a directory.
33
+ // To match the real ls, if we're asked to ls a file, we'll just print
34
+ // out the file's name.
35
+ // https://pkg.go.dev/io/fs#FileInfo
36
+ if fileInfo .IsDir () == false {
37
+ fmt .Fprintln (cmd .OutOrStdout (), fileInfo .Name ())
38
+ return nil
39
+ }
40
+
23
41
// Read this directory to get a list of files
24
42
// https://pkg.go.dev/os#ReadDir
25
43
files , err := os .ReadDir (dir )
Original file line number Diff line number Diff line change @@ -42,3 +42,20 @@ rain.txt
42
42
t .Fatalf ("expected \" %s\" got \" %s\" " , expected , string (out ))
43
43
}
44
44
}
45
+
46
+ func TestRootCmdFile (t * testing.T ) {
47
+ cmd := NewRoodCmd ()
48
+ b := bytes .NewBufferString ("" )
49
+ cmd .SetOut (b )
50
+ cmd .SetArgs ([]string {"../assets/dew.txt" })
51
+ cmd .Execute ()
52
+ out , err := ioutil .ReadAll (b )
53
+ if err != nil {
54
+ t .Fatal (err )
55
+ }
56
+ expected := `dew.txt
57
+ `
58
+ if string (out ) != expected {
59
+ t .Fatalf ("expected \" %s\" got \" %s\" " , expected , string (out ))
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments