Skip to content

Commit

Permalink
chore: lint for GO 1.20 and add workflow (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody authored Oct 3, 2023
1 parent 5ef089c commit 4a3d173
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 13 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/go-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: lint
on:
pull_request:
branches:
- master
paths:
- ".github/workflows/go-lint.yaml"
- "helpers/foundation-deployer/**"
permissions:
contents: read

concurrency:
group: '$${{ github.workflow }}-$${{ github.head_ref || github.ref }}'
cancel-in-progress: true

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
folder: [helpers/foundation-deployer]
steps:
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version-file: ${{ matrix.folder }}/go.mod
cache-dependency-path: ${{ matrix.folder }}/go.sum
- name: golangci-lint
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
with:
version: latest
working-directory: ${{ matrix.folder }}
args: --timeout=5m
2 changes: 1 addition & 1 deletion helpers/foundation-deployer/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/terraform-google-modules/terraform-example-foundation/helpers/foundation-deployer

go 1.18
go 1.20

require (
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.5.0
Expand Down
5 changes: 4 additions & 1 deletion helpers/foundation-deployer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func main() {
}

if cfg.resetStep != "" {
s.ResetStep(cfg.resetStep)
if err := s.ResetStep(cfg.resetStep); err != nil {
fmt.Printf("# Reset step failed. Error: %s\n", err.Error())
os.Exit(3)
}
return
}

Expand Down
7 changes: 5 additions & 2 deletions helpers/foundation-deployer/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ var (
bar = strings.Repeat("#", size)
)

func PressEnter(msg string) error {
func PressEnter(msg string) {
reader := bufio.NewReader(os.Stdin)
t := "# Press Enter to continue"
if msg != "" {
t = msg
}
fmt.Print(t)
_, err := reader.ReadString('\n')
return err
if err != nil {
fmt.Printf("# Failed to read string. Error: %s\n", err.Error())
os.Exit(3)
}
}

func pad(msg string, size int) string {
Expand Down
4 changes: 3 additions & 1 deletion helpers/foundation-deployer/stages/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const (

func DestroyBootstrapStage(t testing.TB, s steps.Steps, c CommonConf) error {

forceBackendMigration(t, BootstrapRepo, "envs", "shared", c)
if err := forceBackendMigration(t, BootstrapRepo, "envs", "shared", c); err != nil {
return err
}

stageConf := StageConf{
Stage: BootstrapStep,
Expand Down
25 changes: 17 additions & 8 deletions helpers/foundation-deployer/steps/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,37 @@ func TestProcessSteps(t *testing.T) {

// Loading a new file
s, err := LoadSteps(filepath.Join(t.TempDir(), "new.json"))
assert.NoError(t, err)

// CompleteStep
assert.False(t, s.IsStepComplete("unit"), "check if 'unit' is 'COMPLETED' should be false")
s.CompleteStep("unit")
err = s.CompleteStep("unit")
assert.NoError(t, err)
assert.True(t, s.IsStepComplete("unit"), "check if 'unit' is 'COMPLETED' should be true")

// FailStep
msg := "step failed"
s.FailStep("fail", msg)
err = s.FailStep("fail", msg)
assert.NoError(t, err)
assert.False(t, s.IsStepComplete("fail"), "check if 'fail' is 'COMPLETED' should be false")
assert.Equal(t, s.GetStepError("fail"), msg, "step should have failed")

// DestroyStep
assert.False(t, s.IsStepDestroyed("old"), "check if 'old' is 'DESTROYED' should be false")
s.DestroyStep("old")
err = s.DestroyStep("old")
assert.NoError(t, err)
assert.True(t, s.IsStepDestroyed("old"), "check if 'old' is 'DESTROYED' should be true")

// ResetStep
s.CompleteStep("reset")
s.CompleteStep("reset.one")
err = s.CompleteStep("reset")
assert.NoError(t, err)
err = s.CompleteStep("reset.one")
assert.NoError(t, err)
assert.True(t, s.IsStepComplete("reset"), "check if 'reset is 'COMPLETED' should be true")
assert.True(t, s.IsStepComplete("reset.one"), "check if 'reset.one' is 'COMPLETED' should be true")

s.ResetStep("reset.one")
err = s.ResetStep("reset.one")
assert.NoError(t, err)

assert.False(t, s.IsStepComplete("reset.one"), "check if 'reset.one' is 'COMPLETED' should be false")
assert.False(t, s.IsStepComplete("reset"), "check if 'reset' is 'COMPLETED' should be false")
Expand Down Expand Up @@ -92,7 +99,8 @@ func TestProcessSteps(t *testing.T) {
assert.NoError(t, err)
assert.True(t, s.IsStepDestroyed("unit"), "check if 'unit' is 'DESTROYED' should be true")

s.CompleteStep("destroy")
err = s.CompleteStep("destroy")
assert.NoError(t, err)
assert.False(t, s.IsStepDestroyed("destroy"), "check if 'destroy' is 'DESTROYED' should be false")
err = s.RunDestroyStep("destroy", func() error {
return fmt.Errorf(badStepMsg)
Expand All @@ -101,7 +109,8 @@ func TestProcessSteps(t *testing.T) {
assert.False(t, s.IsStepDestroyed("destroy"), "check if 'destroy' is 'DESTROYED' should be false")
assert.Equal(t, s.GetStepError("destroy"), badStepMsg, "step 'destroy' should have failed")

s.DestroyStep("gone")
err = s.DestroyStep("gone")
assert.NoError(t, err)
assert.True(t, s.IsStepDestroyed("gone"), "check if 'gone' is 'DESTROYED' should be true")
err = s.RunDestroyStep("gone", func() error {
return fmt.Errorf("will fail if executed")
Expand Down
1 change: 1 addition & 0 deletions helpers/foundation-deployer/utils/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestGit(t *testing.T) {

localBranch, err := local.GetCurrentBranch()
assert.Equal(t, localBranch, "unit-test", "current branch should be 'unit-test'")
assert.NoError(t, err)

err = os.WriteFile(filepath.Join(repo, "go.mod"), []byte("module example.com/test\n"), 0644)
assert.NoError(t, err)
Expand Down

0 comments on commit 4a3d173

Please sign in to comment.