Skip to content

Commit

Permalink
Updated to fix our test failures.
Browse files Browse the repository at this point in the history
I'd forgotten to remove references to the slurpFn, and add help
text for everything.
  • Loading branch information
skx committed Oct 16, 2022
1 parent 1c6f3bd commit e297710
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
87 changes: 43 additions & 44 deletions builtins/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,49 @@ func TestFile(t *testing.T) {

}

// TestFileRead tests file:read
func TestFileRead(t *testing.T) {

// calling with no argument
out := fileReadFn(ENV, []primitive.Primitive{})

// Will lead to an error
_, ok := out.(primitive.Error)
if !ok {
t.Fatalf("expected error, got %v", out)
}

// Call with a file that doesn't exist
out = fileReadFn(ENV, []primitive.Primitive{
primitive.String("path/not/found")})

_, ok = out.(primitive.Error)
if !ok {
t.Fatalf("expected error, got %v", out)
}

// Create a temporary file, and read the contents
tmp, _ := os.CreateTemp("", "yal")
err := os.WriteFile(tmp.Name(), []byte("I like cake"), 0777)
if err != nil {
t.Fatalf("failed to write to file")
}
defer os.Remove(tmp.Name())

str := fileReadFn(ENV, []primitive.Primitive{
primitive.String(tmp.Name())})

// Will lead to an error
txt, ok2 := str.(primitive.String)
if !ok2 {
t.Fatalf("expected string, got %v", out)
}

if txt.ToString() != "I like cake" {
t.Fatalf("re-reading the temporary file gave bogus contents")
}
}

// TestGenSym tests gensym
func TestGenSym(t *testing.T) {

Expand Down Expand Up @@ -2087,50 +2130,6 @@ func TestShell(t *testing.T) {
}
}

// TestSlurp tests slurp
func TestSlurp(t *testing.T) {

// calling with no argument
out := slurpFn(ENV, []primitive.Primitive{})

// Will lead to an error
_, ok := out.(primitive.Error)
if !ok {
t.Fatalf("expected error, got %v", out)
}

// Call with a file that doesn't exist
out = slurpFn(ENV, []primitive.Primitive{
primitive.String("path/not/found")})

_, ok = out.(primitive.Error)
if !ok {
t.Fatalf("expected error, got %v", out)
}

// Create a temporary file, and read the contents
tmp, _ := os.CreateTemp("", "yal")
err := os.WriteFile(tmp.Name(), []byte("I like cake"), 0777)
if err != nil {
t.Fatalf("failed to write to file")
}
defer os.Remove(tmp.Name())

str := slurpFn(ENV, []primitive.Primitive{
primitive.String(tmp.Name())})

// Will lead to an error
txt, ok2 := str.(primitive.String)
if !ok2 {
t.Fatalf("expected string, got %v", out)
}

if txt.ToString() != "I like cake" {
t.Fatalf("re-reading the temporary file gave bogus contents")
}

}

func TestSort(t *testing.T) {

// No arguments
Expand Down
14 changes: 14 additions & 0 deletions builtins/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ More specifically something is regarded as a file if it is NOT a directory.
See also: directory? exists?
Example: (print (file? "/dev/null"))
%%
file:lines

file:lines returns the contents of the given file, as a list of lines

See also: file:read
%%
file:read

file:read returns the contents of the given file, as a string.

See also: file:lines
%%


file:stat

file:stat returns a list containing details of the given file/directory,
Expand Down

0 comments on commit e297710

Please sign in to comment.