Skip to content

Commit

Permalink
Merge pull request #63 from buildpacks/make
Browse files Browse the repository at this point in the history
Add a makefile to the repository to run linting and tests
  • Loading branch information
ekcasey authored Jul 1, 2021
2 parents c07e39f + 66f6023 commit 8290e25
Show file tree
Hide file tree
Showing 24 changed files with 1,214 additions and 82 deletions.
19 changes: 19 additions & 0 deletions .github/pipeline-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,22 @@ github:
codeowners:
- path: "*"
owner: "@buildpacks/implementation-maintainers"

test:
steps:
- name: Install richgo
run: |
#!/usr/bin/env bash
set -euo pipefail
GO111MODULE=on go get -u -ldflags="-s -w" github.com/kyoh86/richgo
- name: Run Tests
run: |
#!/usr/bin/env bash
set -euo pipefail
GOCMD=richgo make
env:
RICHGO_FORCE_COLOR: "1"
3 changes: 0 additions & 3 deletions .github/pipelines-descriptor.yml

This file was deleted.

16 changes: 2 additions & 14 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ jobs:
set -euo pipefail
echo "Installing richgo ${RICHGO_VERSION}"
mkdir -p "${HOME}"/bin
echo "${HOME}/bin" >> "${GITHUB_PATH}"
curl \
--location \
--show-error \
--silent \
"https://github.com/kyoh86/richgo/releases/download/v${RICHGO_VERSION}/richgo_${RICHGO_VERSION}_linux_amd64.tar.gz" \
| tar -C "${HOME}"/bin -xz richgo
env:
RICHGO_VERSION: 0.3.6
GO111MODULE=on go get -u -ldflags="-s -w" github.com/kyoh86/richgo
- name: Run Tests
run: |
#!/usr/bin/env bash
set -euo pipefail
richgo test ./...
GOCMD=richgo make
env:
RICHGO_FORCE_COLOR: "1"
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Go parameters
GOCMD?=go
GO_VERSION=$(shell go list -m -f "{{.GoVersion}}")
PACKAGE_BASE=github.com/buildpacks/libcnb

all: test

install-goimports:
@echo "> Installing goimports..."
cd tools && $(GOCMD) install golang.org/x/tools/cmd/goimports

format: install-goimports
@echo "> Formating code..."
@goimports -l -w -local ${PACKAGE_BASE} .

install-golangci-lint:
@echo "> Installing golangci-lint..."
cd tools && $(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint

lint: install-golangci-lint
@echo "> Linting code..."
@golangci-lint run -c golangci.yaml

test: format lint
$(GOCMD) test -parallel=1 -count=1 -v ./...
20 changes: 10 additions & 10 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test-key = "test-value"
err = buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.6"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())

f, err := ioutil.TempFile("", "build-buildpackplan-path")
Expect(err).NotTo(HaveOccurred())
Expand All @@ -124,7 +124,7 @@ version = "test-version"
[entries.metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

commandPath = filepath.Join("bin", "build")
Expand All @@ -145,18 +145,18 @@ test-key = "test-value"
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

platformPath, err = ioutil.TempDir("", "build-platform-path")
Expect(err).NotTo(HaveOccurred())

Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
[]byte("test-secret-value"), 0644)).To(Succeed())
[]byte("test-secret-value"), 0600)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0644)).
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
To(Succeed())

tomlWriter = &mocks.TOMLWriter{}
Expand Down Expand Up @@ -192,7 +192,7 @@ id = "test-id"
name = "test-name"
version = "1.1.1"
`),
0644),
0600),
).To(Succeed())
})

Expand Down Expand Up @@ -404,7 +404,7 @@ version = "1.1.1"
err := buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.5"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())

layer := libcnb.Layer{
Name: "test-name",
Expand Down Expand Up @@ -561,9 +561,9 @@ version = "1.1.1"
})

it("removes stale layers", func() {
Expect(ioutil.WriteFile(filepath.Join(layersPath, "alpha.toml"), []byte(""), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "bravo.toml"), []byte(""), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "store.toml"), []byte(""), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "alpha.toml"), []byte(""), 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "bravo.toml"), []byte(""), 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "store.toml"), []byte(""), 0600)).To(Succeed())

layer := libcnb.Layer{Name: "alpha"}
layerContributor.On("Contribute", mock.Anything).Return(layer, nil)
Expand Down
8 changes: 4 additions & 4 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mixins = ["test-name"]
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

f, err := ioutil.TempFile("", "detect-buildplan-path")
Expand All @@ -108,10 +108,10 @@ test-key = "test-value"

Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
[]byte("test-secret-value"), 0644)).To(Succeed())
[]byte("test-secret-value"), 0600)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0644)).
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
To(Succeed())

tomlWriter = &mocks.TOMLWriter{}
Expand Down Expand Up @@ -146,7 +146,7 @@ id = "test-id"
name = "test-name"
version = "1.1.1"
`),
0644),
0600),
).To(Succeed())
})

Expand Down
1 change: 0 additions & 1 deletion environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,4 @@ func testEnvironment(t *testing.T, context spec.G, it spec.S) {
filepath.Join("test-process", "TEST_NAME.prepend"): "test-value",
}))
})

}
2 changes: 0 additions & 2 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,5 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {

Expect(libcnb.PlatformFormatter(plat).String()).To(Equal("Platform contents: [. test-file]"))
})

})

}
31 changes: 31 additions & 0 deletions golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
run:
timeout: 6m

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- dogsled
- exportloopref
- gocritic
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- varcheck
- whitespace

linters-settings:
goimports:
local-prefixes: github.com/buildpacks/libcnb
6 changes: 3 additions & 3 deletions internal/config_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func testConfigMap(t *testing.T, context spec.G, it spec.S) {
})

it("loads the ConfigMap from a directory", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "test-key"), []byte("test-value"), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "test-key"), []byte("test-value"), 0600)).To(Succeed())

cm, err := internal.NewConfigMapFromPath(path)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -69,7 +69,7 @@ func testConfigMap(t *testing.T, context spec.G, it spec.S) {
Expect(ioutil.WriteFile(
filepath.Join(path, ".hidden", "test-key"),
[]byte("test-value"),
0644,
0600,
)).To(Succeed())
Expect(os.Symlink(
filepath.Join(".hidden", "test-key"),
Expand All @@ -82,7 +82,7 @@ func testConfigMap(t *testing.T, context spec.G, it spec.S) {
})

it("ignores hidden files", func() {
Expect(ioutil.WriteFile(filepath.Join(path, ".hidden-key"), []byte("hidden-value"), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, ".hidden-key"), []byte("hidden-value"), 0600)).To(Succeed())

cm, err := internal.NewConfigMapFromPath(path)
Expect(err).NotTo(HaveOccurred())
Expand Down
1 change: 1 addition & 0 deletions internal/environment_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (w EnvironmentWriter) Write(path string, environment map[string]string) err

for key, value := range environment {
f := filepath.Join(path, key)
// #nosec
if err := ioutil.WriteFile(f, []byte(value), 0644); err != nil {
return fmt.Errorf("unable to write file %s\n%w", f, err)
}
Expand Down
2 changes: 0 additions & 2 deletions internal/environment_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {
path, err = ioutil.TempDir("", "environment-writer")
Expect(err).NotTo(HaveOccurred())
Expect(os.RemoveAll(path)).To(Succeed())

writer = internal.EnvironmentWriter{}
})

it.After(func() {
Expand Down
1 change: 1 addition & 0 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

"github.com/BurntSushi/toml"

"github.com/buildpacks/libcnb/internal"
)

Expand Down
7 changes: 3 additions & 4 deletions layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func testLayer(t *testing.T, context spec.G, it spec.S) {
})

context("Layers", func() {

it.Before(func() {
var err error
path, err = ioutil.TempDir("", "layers")
Expand Down Expand Up @@ -121,7 +120,7 @@ build = false
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

l, err := layers.Layer("test-name")
Expand All @@ -143,7 +142,7 @@ build = false
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

l, err := layers.Layer("test-name")
Expand All @@ -166,7 +165,7 @@ cache = false
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

l, err := layers.Layer("test-name")
Expand Down
13 changes: 6 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mixins = ["test-name"]
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

f, err := ioutil.TempFile("", "main-buildpackplan-path")
Expand All @@ -103,7 +103,7 @@ version = "test-version"
[entries.metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

f, err = ioutil.TempFile("", "main-buildplan-path")
Expand All @@ -129,7 +129,7 @@ test-key = "test-value"
[metadata]
test-key = "test-value"
`),
0644),
0600),
).To(Succeed())

platformPath, err = ioutil.TempDir("", "main-platform-path")
Expand All @@ -139,17 +139,17 @@ test-key = "test-value"
Expect(ioutil.WriteFile(
filepath.Join(platformPath, "bindings", "alpha", "metadata", "test-metadata-key"),
[]byte("test-metadata-value"),
0644,
0600,
)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha", "secret"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(
filepath.Join(platformPath, "bindings", "alpha", "secret", "test-secret-key"),
[]byte("test-secret-value"),
0644,
0600,
)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0644)).
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
To(Succeed())

tomlWriter = &mocks.TOMLWriter{}
Expand Down Expand Up @@ -239,5 +239,4 @@ test-key = "test-value"

Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("unsupported command test-command"))
})

}
3 changes: 2 additions & 1 deletion mocks/builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion mocks/detector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8290e25

Please sign in to comment.