Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 21, 2024
1 parent e8e0d95 commit e4aa7a8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion runny/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Run() {
runny, err := readConfig()
runny, err := readConfig(".runny.yaml")
if err != nil {
color.Red("Problem reading config: %v", err)
}
Expand Down
11 changes: 11 additions & 0 deletions runny/fixtures/simple-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
shell: /bin/bash
commands:
foo:
run: ls foo
bar:
needs:
- foo
run: ls bar
baz:
if: test -e foo.txt
run: ls baz
4 changes: 2 additions & 2 deletions runny/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func commandStringToSingleLine(command string, maxlength int) string {
return result
}

func readConfig() (Config, error) {
func readConfig(path string) (Config, error) {
// Read .runny.yaml from the current directory
var conf Config
yamlFile, err := os.ReadFile(".runny.yaml")
yamlFile, err := os.ReadFile(path)
if err != nil {
return conf, err
}
Expand Down
47 changes: 47 additions & 0 deletions runny/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package runny

import (
"testing"
)

type Input struct {
command string
maxLength int
}

type Param struct {
input Input
expected string
}

func TestCommandStringToSingleLine(t *testing.T) {
params := []Param{
{input: Input{command: "", maxLength: 80}, expected: ""},
{input: Input{command: "foo bar wibble", maxLength: 10}, expected: "foo bar..."},
{input: Input{command: "foo\nbar", maxLength: 80}, expected: "foo; bar"},
{input: Input{command: " foo \n bar ", maxLength: 80}, expected: "foo; bar"},
}
for _, p := range params {
output := commandStringToSingleLine(p.input.command, p.input.maxLength)
if output != p.expected {
t.Fatalf("Expected %s, got %s\n", p.expected, output)
}
}
}

func TestReadConfig(t *testing.T) {
conf, err := readConfig("fixtures/simple-good.yaml")
if err != nil {
t.Fatalf("Got error when reading in config file: %v\n", err)
}

expectedLenCommands := 3
if len(conf.Commands) != expectedLenCommands {
t.Fatalf("Expected %d commands, got %d\n", expectedLenCommands, len(conf.Commands))
}

expectedCommandFooRun := "ls foo"
if conf.Commands["foo"].Run != expectedCommandFooRun {
t.Fatalf("Expected foo command's run value to be %s, got %s", expectedCommandFooRun, conf.Commands["foo"].Run)
}
}

0 comments on commit e4aa7a8

Please sign in to comment.