Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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 }}
69 changes: 69 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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'")
}
}
24 changes: 24 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}