diff --git a/runny/cmd.go b/runny/cmd.go index 4ffeec0..7ade2a7 100644 --- a/runny/cmd.go +++ b/runny/cmd.go @@ -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) } diff --git a/runny/fixtures/simple-good.yaml b/runny/fixtures/simple-good.yaml new file mode 100644 index 0000000..fc5da9d --- /dev/null +++ b/runny/fixtures/simple-good.yaml @@ -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 diff --git a/runny/utils.go b/runny/utils.go index 8d1c06e..02d475d 100644 --- a/runny/utils.go +++ b/runny/utils.go @@ -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 } diff --git a/runny/utils_test.go b/runny/utils_test.go new file mode 100644 index 0000000..28cc9cb --- /dev/null +++ b/runny/utils_test.go @@ -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) + } +}