From 3648f1bfa1b93946ab0131d5dbeec7c49130f4f8 Mon Sep 17 00:00:00 2001 From: subash adhikari Date: Sun, 12 Feb 2023 13:33:11 +1100 Subject: [PATCH] fix diff script parsing issue (#127) * fix failing tests * test script * diff command supports eval * enable logging in tests * upgrade version in readme --- .buildkite/pipeline.yml | 4 ++-- README.md | 8 ++++---- main.go | 4 ++++ main_test.go | 4 ---- pipeline.go | 13 +++++-------- pipeline_test.go | 10 ++++++++++ tests/command.bats | 4 +--- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 2f21fa2..20cc6a2 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -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: @@ -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/" diff --git a/README.md b/README.md index 9b574a2..d35ab10 100644 --- a/README.md +++ b/README.md @@ -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/" @@ -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: @@ -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: @@ -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/ diff --git a/main.go b/main.go index 77fba2d..59afb46 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/main_test.go b/main_test.go index 5c7fe73..b621415 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "os" "testing" @@ -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") diff --git a/pipeline.go b/pipeline.go index d260c0d..6b2b3e9 100644 --- a/pipeline.go +++ b/pipeline.go @@ -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) { diff --git a/pipeline_test.go b/pipeline_test.go index fd199c8..83d6043 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -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"} diff --git a/tests/command.bats b/tests/command.bats index 17c9d18..b64fec5 100644 --- a/tests/command.bats +++ b/tests/command.bats @@ -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: