From a2494294454004077ecaacb8a49e2994af2a6091 Mon Sep 17 00:00:00 2001 From: Lawrence Date: Sat, 2 Nov 2024 08:11:38 +0000 Subject: [PATCH] test: add tests for command package --- .github/workflows/quality-checks.yml | 42 ++++++++++------ .../command/inject_layer_environment_test.go | 44 +++++++++++++++++ common/command/make_test.go | 33 +++++++++++++ common/command/run_test.go | 29 +++++++++++ go.mod | 19 +++---- go.sum | 49 +++++++++---------- 6 files changed, 166 insertions(+), 50 deletions(-) create mode 100644 common/command/inject_layer_environment_test.go create mode 100644 common/command/make_test.go create mode 100644 common/command/run_test.go diff --git a/.github/workflows/quality-checks.yml b/.github/workflows/quality-checks.yml index b426bcc..5468824 100644 --- a/.github/workflows/quality-checks.yml +++ b/.github/workflows/quality-checks.yml @@ -5,7 +5,20 @@ run-name: ๐Ÿ”ฌ @${{ github.triggering_actor }} is checking quality on ${{ github on: [push] jobs: - enumerate-buildpack-tests: + unit-tests: + name: ๐Ÿงช unit tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: echo "version=$(grep -Ee 'go [0-9\.]+' go.mod | awk '{ print $2; }')" >> $GITHUB_OUTPUT + id: go_mod + - uses: actions/setup-go@v5 + with: + go-version: ${{ steps.go_mod.outputs.version }} + cache-dependency-path: "**/*.sum" + - run: go test ./... + + enumerate-integration-tests: name: ๐Ÿ”Ž enumerate buildpack tests runs-on: ubuntu-latest outputs: @@ -21,25 +34,26 @@ jobs: const {join, relative} = require("node:path"); return await readdir(process.cwd()) - .then(dirs => Promise.all(dirs.map(async d => { - try { - await stat(join(process.cwd(), d, 'buildpack.toml')); - return relative(process.cwd(), join(process.cwd(), d)); - } catch (e) { - return null; - } - }))) + .then(dirs => Promise.all(dirs.map(async d => + stat(join(process.cwd(), d, 'buildpack.toml')) + .then(() => relative(process.cwd(), join(process.cwd(), d))) + .catch(() => null)) + )) .then(dirs => dirs.filter(d => Boolean(d))) - .then(dirs => Promise.all(dirs.map(async d => { - const makefile = (await readFile(join(process.cwd(), d, 'Makefile'))).toString(); - return makefile.match(/^test-[^:]+/mg)?.map(t => `${d}:${t}`); - }))) + .then(dirs => Promise.all(dirs.map(async d => + readFile(join(process.cwd(), d, 'Makefile')) + .then(makefile => { + const matches = makefile.toString().match(/^test-[^:]+/mg); + return matches ? matches.map(test => `${d}:${test}`) : null; + }) + .catch(() => null)) + )) .then(dirs => dirs.flat()) .then(dirs => dirs.filter(d => Boolean(d))); integration-tests: name: โš™๏ธ integration test ${{ matrix.buildpack-test }} - needs: [ enumerate-buildpack-tests ] + needs: [ enumerate-integration-tests ] runs-on: ubuntu-latest strategy: fail-fast: false diff --git a/common/command/inject_layer_environment_test.go b/common/command/inject_layer_environment_test.go new file mode 100644 index 0000000..6ef7710 --- /dev/null +++ b/common/command/inject_layer_environment_test.go @@ -0,0 +1,44 @@ +package command + +import ( + "bytes" + "github.com/buildpacks/libcnb" + "strings" + "testing" +) + +func TestInjectingALayerEnvironmentWithPrepend(t *testing.T) { + writer := new(bytes.Buffer) + command := Make(writer, "env") + + environment := make(libcnb.Environment) + environment["TEST_ENV.prepend"] = "A_TEST_VARIABLE" + + InjectLayerEnvironment(command, environment) + + err := command.Run() + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "TEST_ENV") && !strings.Contains(writer.String(), "A_TEST_VARIABLE") { + t.Fatalf("Output did not contain TEST_ENV") + } +} + +func TestInjectingALayerEnvironmentWithAppend(t *testing.T) { + writer := new(bytes.Buffer) + command := Make(writer, "env") + + environment := make(libcnb.Environment) + environment["TEST_ENV.append"] = "A_TEST_VARIABLE" + + InjectLayerEnvironment(command, environment) + + err := command.Run() + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "TEST_ENV") && !strings.Contains(writer.String(), "A_TEST_VARIABLE") { + t.Fatalf("Output did not contain TEST_ENV") + } +} diff --git a/common/command/make_test.go b/common/command/make_test.go new file mode 100644 index 0000000..71a81d7 --- /dev/null +++ b/common/command/make_test.go @@ -0,0 +1,33 @@ +package command + +import ( + "bytes" + "strings" + "testing" +) + +func TestMakingACommandWithoutArguments(t *testing.T) { + writer := new(bytes.Buffer) + command := Make(writer, "ls") + + err := command.Run() + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "make_test.go") { + t.Fatalf("Output did not contain make_test.go") + } +} + +func TestMakingACommandWithArguments(t *testing.T) { + writer := new(bytes.Buffer) + command := Make(writer, "ls", "../") + + err := command.Run() + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "command") { + t.Fatalf("Output did not contain command") + } +} diff --git a/common/command/run_test.go b/common/command/run_test.go new file mode 100644 index 0000000..3aa14b2 --- /dev/null +++ b/common/command/run_test.go @@ -0,0 +1,29 @@ +package command + +import ( + "bytes" + "strings" + "testing" +) + +func TestRunningACommandWithoutArguments(t *testing.T) { + writer := new(bytes.Buffer) + err := Run(writer, "ls") + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "make_test.go") { + t.Fatalf("Output did not contain make_test.go") + } +} + +func TestRunningACommandWithArguments(t *testing.T) { + writer := new(bytes.Buffer) + err := Run(writer, "ls", "../") + if err != nil { + t.Errorf("%s", err) + } + if !strings.Contains(writer.String(), "command") { + t.Fatalf("Output did not contain command") + } +} diff --git a/go.mod b/go.mod index 402c438..243bc82 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,23 @@ module github.com/acodeninja/buildpacks -go 1.22.1 +go 1.23 + +toolchain go1.23.2 require ( - github.com/buildpacks/libcnb v1.30.3 - github.com/fatih/color v1.16.0 - github.com/paketo-buildpacks/libpak v1.69.1 + github.com/BurntSushi/toml v1.4.0 + github.com/buildpacks/libcnb v1.30.4 + github.com/paketo-buildpacks/libpak v1.72.0 ) require ( - github.com/BurntSushi/toml v1.3.2 // indirect - github.com/Masterminds/semver/v3 v3.2.1 // indirect - github.com/creack/pty v1.1.21 // indirect + github.com/Masterminds/semver/v3 v3.3.0 // indirect + github.com/creack/pty v1.1.24 // indirect github.com/heroku/color v0.0.6 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect - github.com/onsi/gomega v1.33.0 // indirect - golang.org/x/sys v0.18.0 // indirect + github.com/onsi/gomega v1.35.1 // indirect + golang.org/x/sys v0.26.0 // indirect ) diff --git a/go.sum b/go.sum index 266baf9..05e9c18 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,21 @@ -github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= -github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= -github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= -github.com/buildpacks/libcnb v1.30.1 h1:airpkZzh8Mlt+SQEMTj0B1r5agDHZvgfpwzpKVMEgZQ= -github.com/buildpacks/libcnb v1.30.1/go.mod h1:JU2fam/ECXw6I5iRnZbfKgjWsrZbzbgvZHR6WBSVHRc= -github.com/buildpacks/libcnb v1.30.3 h1:JtFMFPO2450uDLzpE1b50TvmM1GEZa8YT9cQ2ZZtHqA= -github.com/buildpacks/libcnb v1.30.3/go.mod h1:JPb1vC7HQcGK0oONfqJvsYzOjw3be+WBbQ0KYOIhNvA= -github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0= -github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0= +github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= +github.com/buildpacks/libcnb v1.30.4 h1:Jp6cJxYsZQgqix+lpRdSpjHt5bv5yCJqgkw9zWmS6xU= +github.com/buildpacks/libcnb v1.30.4/go.mod h1:vjEDAlK3/Rf67AcmBzphXoqIlbdFgBNUK5d8wjreJbY= +github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= +github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= -github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/heroku/color v0.0.6 h1:UTFFMrmMLFcL3OweqP1lAdp8i1y/9oHqkeHjQ/b/Ny0= github.com/heroku/color v0.0.6/go.mod h1:ZBvOcx7cTF2QKOv4LbmoBtNl5uB17qWxGuzZrsi1wLU= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= +github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= @@ -27,14 +25,10 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= -github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo= -github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0= -github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE= -github.com/onsi/gomega v1.33.0/go.mod h1:+925n5YtiFsLzzafLUHzVMBpvvRAzrydIBiSIxjX3wY= -github.com/paketo-buildpacks/libpak v1.69.0 h1:AUn68IDq9zTMNJkmG+dx4pO/6mlwevmTWYLVYxpdptU= -github.com/paketo-buildpacks/libpak v1.69.0/go.mod h1:sGW2nG+QL8jtQrIpr3Rluo3dGIKZRQbVkQBqrj94Bnc= -github.com/paketo-buildpacks/libpak v1.69.1 h1:C+4I0jwu6PA7jQxX4NNVrsm8cvLSdfKWaulPYFVvR68= -github.com/paketo-buildpacks/libpak v1.69.1/go.mod h1:gGggptzbTUIG+RVayiqYlCDiB+W8RfRCiN44MUsOttI= +github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4= +github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog= +github.com/paketo-buildpacks/libpak v1.72.0 h1:is/JJzMM7vJSI/RghROSj1qZJPtkTSKfOlsceTNeEhw= +github.com/paketo-buildpacks/libpak v1.72.0/go.mod h1:LHep340UxOKfCZuyT9Fho5HSStkE1iWt7+P52KhAm0Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= @@ -43,16 +37,17 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= -golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=