From 9fb288687a79e1fb955ef303edc8f925947b31e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ljubo=20Nikoli=C4=87?= Date: Tue, 4 Jun 2024 14:05:10 +0200 Subject: [PATCH 1/4] add unit tests --- cmd/root_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/server_test.go | 24 ++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 cmd/root_test.go create mode 100644 cmd/server_test.go diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..48691b8 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,69 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/cobra" +) + +func TestIsDebugMode(t *testing.T) { + rootConfig.Debug = true + if !IsDebugMode() { + t.Errorf("IsDebugMode() = %v; want true", IsDebugMode()) + } + + rootConfig.Debug = false + if IsDebugMode() { + t.Errorf("IsDebugMode() = %v; want false", IsDebugMode()) + } +} + +func TestRootCmd(t *testing.T) { + if RootCmd.Use != "tfe-plan-bot" { + t.Errorf("RootCmd.Use = %v; want 'tfe-plan-bot'", RootCmd.Use) + } + + if RootCmd.Short != "A bot for creating TFE speculative plans on Github pull requests." { + t.Errorf("RootCmd.Short = %v; want 'A bot for creating TFE speculative plans on Github pull requests.'", RootCmd.Short) + } + + if RootCmd.Long != "A bot for creating TFE speculative plans on Github pull requests, when a given policy is satisfied." { + t.Errorf("RootCmd.Long = %v; want 'A bot for creating TFE speculative plans on Github pull requests, when a given policy is satisfied.'", RootCmd.Long) + } + + if RootCmd.SilenceUsage != true { + t.Errorf("RootCmd.SilenceUsage = %v; want true", RootCmd.SilenceUsage) + } +} + +func TestRootCmdDebugFlag(t *testing.T) { + cmd := &cobra.Command{} + cmd.Flags().AddFlagSet(RootCmd.PersistentFlags()) + + if cmd.Flag("debug") == nil { + t.Errorf("Expected debug flag to be defined") + } +} + +func TestInit(t *testing.T) { + cmd := &cobra.Command{ + Use: "tfe-plan-bot", + } + cmd.PersistentFlags().BoolVarP(&rootConfig.Debug, "debug", "d", false, "enables debug output") + + if cmd.Flag("debug") == nil { + t.Errorf("Expected debug flag to be defined") + } + + if cmd.Flag("debug").Shorthand != "d" { + t.Errorf("Expected debug flag shorthand to be 'd'") + } + + if cmd.Flag("debug").Usage != "enables debug output" { + t.Errorf("Expected debug flag usage to be 'enables debug output'") + } + + if cmd.Flag("debug").DefValue != "false" { + t.Errorf("Expected debug flag default value to be 'false'") + } +} \ No newline at end of file diff --git a/cmd/server_test.go b/cmd/server_test.go new file mode 100644 index 0000000..ab25adc --- /dev/null +++ b/cmd/server_test.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/cobra" +) + +func TestServerCmd(t *testing.T) { + cmd := &cobra.Command{ + Use: "tfe-plan-bot", + } + serverCmdConfig.Path = "nonexistent.yml" + + err := serverCmd(cmd, []string{}) + if err == nil { + t.Errorf("Expected error due to nonexistent config file, got nil") + } + + expectedError := "failed to read server config: failed fetching server config file: nonexistent.yml: stat nonexistent.yml: no such file or directory" + if err.Error() != expectedError { + t.Errorf("Expected error '%s', got '%s'", expectedError, err.Error()) + } +} \ No newline at end of file From bbb6ec97dffb7bcec6d02f528b808d5362844abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ljubo=20Nikoli=C4=87?= Date: Tue, 4 Jun 2024 14:05:22 +0200 Subject: [PATCH 2/4] remove deprecated packages --- server/config.go | 2 +- server/server.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/config.go b/server/config.go index a4e314e..8abc633 100644 --- a/server/config.go +++ b/server/config.go @@ -20,7 +20,7 @@ import ( "github.com/c2h5oh/datasize" "github.com/palantir/go-baseapp/baseapp" - "github.com/palantir/go-baseapp/baseapp/datadog" + "github.com/palantir/go-baseapp/appmetrics/emitter/datadog" "github.com/palantir/go-githubapp/githubapp" "github.com/pkg/errors" "gopkg.in/yaml.v2" diff --git a/server/server.go b/server/server.go index b310b05..91ec846 100644 --- a/server/server.go +++ b/server/server.go @@ -25,7 +25,7 @@ import ( "github.com/die-net/lrucache" "github.com/gregjones/httpcache" "github.com/palantir/go-baseapp/baseapp" - "github.com/palantir/go-baseapp/baseapp/datadog" + "github.com/palantir/go-baseapp/appmetrics/emitter/datadog" "github.com/palantir/go-githubapp/githubapp" "github.com/palantir/policy-bot/pull" "github.com/pkg/errors" From 7dd08a9d9c2a00e13b57aabe8fbf5efb12e14fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ljubo=20Nikoli=C4=87?= Date: Tue, 4 Jun 2024 14:05:48 +0200 Subject: [PATCH 3/4] refactor CI to support build & tests run --- .github/workflows/ci.yml | 40 +++++++++++++++++++++-------------- .github/workflows/release.yml | 35 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 503746c..d9f339a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,31 +1,39 @@ -name: Release +name: CI on: push: - tags: - - 'v*' + pull_request: + branches: + - main +env: + GO_VERSION: 1.22 + GORELEASER_VERSION: v1.25.1 + jobs: - release: + build: + name: Build runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '1.22' - - name: Login to DockerHub - uses: docker/login-action@v3 + go-version: ${{ env.GO_VERSION }} + - name: Cache Go modules + uses: actions/cache@v4 with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_TOKEN }} - - name: Run goreleaser + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Install goreleaser uses: goreleaser/goreleaser-action@v5 with: - version: v1.25.1 - args: release --clean - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + version: ${{ env.GORELEASER_VERSION }} + install-only: true + - name: Build + run: goreleaser build --clean --snapshot + - name: Run tests + run: go test -v ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d1d9970 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,35 @@ +name: Release + +on: + push: + tags: + - 'v*' + +env: + GO_VERSION: 1.22 + GORELEASER_VERSION: v1.25.1 + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + - name: Run goreleaser + uses: goreleaser/goreleaser-action@v5 + with: + version: ${{ env.GORELEASER_VERSION }} + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 47be59da8ec709cdc94c9598fb305361ea965d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ljubo=20Nikoli=C4=87?= Date: Tue, 4 Jun 2024 14:30:56 +0200 Subject: [PATCH 4/4] Revert "remove deprecated packages" This reverts commit bbb6ec97dffb7bcec6d02f528b808d5362844abb. --- server/config.go | 2 +- server/server.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/config.go b/server/config.go index 8abc633..a4e314e 100644 --- a/server/config.go +++ b/server/config.go @@ -20,7 +20,7 @@ import ( "github.com/c2h5oh/datasize" "github.com/palantir/go-baseapp/baseapp" - "github.com/palantir/go-baseapp/appmetrics/emitter/datadog" + "github.com/palantir/go-baseapp/baseapp/datadog" "github.com/palantir/go-githubapp/githubapp" "github.com/pkg/errors" "gopkg.in/yaml.v2" diff --git a/server/server.go b/server/server.go index 91ec846..b310b05 100644 --- a/server/server.go +++ b/server/server.go @@ -25,7 +25,7 @@ import ( "github.com/die-net/lrucache" "github.com/gregjones/httpcache" "github.com/palantir/go-baseapp/baseapp" - "github.com/palantir/go-baseapp/appmetrics/emitter/datadog" + "github.com/palantir/go-baseapp/baseapp/datadog" "github.com/palantir/go-githubapp/githubapp" "github.com/palantir/policy-bot/pull" "github.com/pkg/errors"