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 }} 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