Skip to content

Commit

Permalink
fix diff script parsing issue (adikari#127)
Browse files Browse the repository at this point in the history
* fix failing tests

* test script

* diff command supports eval

* enable logging in tests

* upgrade version in readme
  • Loading branch information
adikari authored Feb 12, 2023
1 parent 159143d commit 3648f1b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ steps:

- label: ":bomb: Testing groups"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "cat ./e2e/multiple-paths"
watch:
- path:
Expand All @@ -46,7 +46,7 @@ steps:

- label: ":bomb: Testing hooks"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "cat ./e2e/multiple-paths"
watch:
- path: "user-service/"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ If the version number is not provided then the most recent version of the plugin
steps:
- label: "Triggering pipelines"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "git diff --name-only HEAD~1"
watch:
- path: "bar-service/"
Expand All @@ -35,7 +35,7 @@ steps:
steps:
- label: "Triggering pipelines"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "git diff --name-only $(head -n 1 last_successful_build)"
interpolation: false
env:
Expand Down Expand Up @@ -151,7 +151,7 @@ Add `log_level` property to set the log level. Supported log levels are `debug`
steps:
- label: "Triggering pipelines"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "git diff --name-only HEAD~1"
log_level: "debug" # defaults to "info"
watch:
Expand Down Expand Up @@ -227,7 +227,7 @@ hooks:
steps:
- label: "Triggering pipelines"
plugins:
- monebag/monorepo-diff#v2.5.6:
- monebag/monorepo-diff#v2.5.7:
diff: "git diff --name-only HEAD~1"
watch:
- path: app/cms/
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func main() {

setupLogger(plugin.LogLevel)

if env("BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE", "false") == "true" {
return
}

if _, _, err = uploadPipeline(plugin, generatePipeline); err != nil {
log.Fatalf("+++ failed to upload pipeline: %v", err)
}
Expand Down
4 changes: 0 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"testing"

Expand All @@ -12,9 +11,6 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

// disable logs in test
log.SetOutput(ioutil.Discard)

// set some env variables for using in tests
os.Setenv("BUILDKITE_COMMIT", "123")
os.Setenv("BUILDKITE_MESSAGE", "fix: temp file not correctly deleted")
Expand Down
13 changes: 5 additions & 8 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,16 @@ func uploadPipeline(plugin Plugin, generatePipeline PipelineGenerator) (string,
func diff(command string) ([]string, error) {
log.Infof("Running diff command: %s", command)

split := strings.Split(command, " ")
cmd, args := split[0], split[1:]
output, err := executeCommand(
env("SHELL", "bash"),
[]string{"-c", strings.Replace(command, "\n", " ", -1)},
)

output, err := executeCommand(cmd, args)
if err != nil {
return nil, fmt.Errorf("diff command failed: %v", err)
}

f := func(c rune) bool {
return c == '\n'
}

return strings.FieldsFunc(strings.TrimSpace(output), f), nil
return strings.Fields(strings.TrimSpace(output)), nil
}

func stepsToTrigger(files []string, watch []WatchConfig) ([]Step, error) {
Expand Down
10 changes: 10 additions & 0 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ README.md`)
assert.Equal(t, want, got)
}

func TestDiffWithSubshell(t *testing.T) {
want := []string{
"user-service/infrastructure/cloudfront.yaml",
"user-service/serverless.yaml",
}
got, err := diff("echo $(cat e2e/multiple-paths)")
assert.NoError(t, err)
assert.Equal(t, want, got)
}

func TestPipelinesToTriggerGetsListOfPipelines(t *testing.T) {
want := []string{"service-1", "service-2", "service-4"}

Expand Down
4 changes: 1 addition & 3 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ EOM

assert_success

assert_output --partial "--- running monorepo-diff"
assert_output --partial "Running diff command: echo foo-service/ \nbat-service/"
assert_output --partial "Output from diff: \nfoo-service/ \nbat-service/"
assert_output --partial "--- running monorepo-diff-buildkite-plugin"

assert_output --partial << EOM
steps:
Expand Down

0 comments on commit 3648f1b

Please sign in to comment.