From 1bb9c113e25916cdb4d5e5ac7077a77ccd6f697b Mon Sep 17 00:00:00 2001 From: Charles Francoise Date: Tue, 13 Jun 2023 09:33:50 +0200 Subject: [PATCH 1/2] create node project testdata this is used for `circleci config generate` tests --- cmd/testdata/node/package.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 cmd/testdata/node/package.json diff --git a/cmd/testdata/node/package.json b/cmd/testdata/node/package.json new file mode 100644 index 000000000..00e66575a --- /dev/null +++ b/cmd/testdata/node/package.json @@ -0,0 +1,10 @@ +{ + "name": "circleci-cli-node-test", + "version": "1.0.0", + "private": true, + "scripts": { + "build": "webpack", + "lint": "eslint .", + "test": "jest ." + } +} From b7555a903ee1157e27e774844548c3356c2d9386 Mon Sep 17 00:00:00 2001 From: Charles Francoise Date: Tue, 13 Jun 2023 09:34:08 +0200 Subject: [PATCH 2/2] use os.Getwd instead of `"."` --- cmd/config.go | 11 +++++++++-- cmd/config_test.go | 24 +++++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 44e18b1a4..f20b9add1 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -162,8 +162,15 @@ func migrateConfig(args []string) error { } func generateConfig(args []string) error { - path := "." - if len(args) == 1 { + var err error + var path string + if len(args) == 0 { + // use working directory as default + path, err = os.Getwd() + if err != nil { + return fmt.Errorf("couldn't get working directory") + } + } else { path = args[0] } diff --git a/cmd/config_test.go b/cmd/config_test.go index 6bd041cff..3b1fb3089 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -2,6 +2,7 @@ package cmd_test import ( "fmt" + "os" "os/exec" "path/filepath" @@ -245,22 +246,35 @@ var _ = Describe("Config", func() { }) }) }) + Describe("generate", func() { It("works without a path", func() { command := exec.Command(pathCLI, "config", "generate") + command.Dir = "testdata/node" session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) - session.Wait() Expect(err).ShouldNot(HaveOccurred()) + + session.Wait() + Eventually(session.Err.Contents()).Should(BeEmpty()) - Eventually(session.Out.Contents()).Should(MatchRegexp("#.*")) + Eventually(session.Out.Contents()).Should(MatchRegexp("npm run test")) + Eventually(session).Should(gexec.Exit(0)) }) + It("works with a path", func() { - command := exec.Command(pathCLI, "config", "generate", "..") + wd, err := os.Getwd() + Expect(err).ShouldNot(HaveOccurred()) + + command := exec.Command(pathCLI, "config", "generate", "node") + command.Dir = filepath.Join(wd, "testdata") session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) - session.Wait() Expect(err).ShouldNot(HaveOccurred()) + + session.Wait() + Eventually(session.Err.Contents()).Should(BeEmpty()) - Eventually(session.Out.Contents()).Should(MatchRegexp("#.*")) + Eventually(session.Out.Contents()).Should(MatchRegexp("npm run test")) + Eventually(session).Should(gexec.Exit(0)) }) }) })