Skip to content

Commit

Permalink
test: add wip cli integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Jan 20, 2025
1 parent 4e19c3c commit 759fe17
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ jobs:
- name: go test
run: go test -v ./...

integration:
needs: [test]
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Run integration tests
env:
INTEGRATION: "1"
run: |
go test ./tests/... -v
live:
needs: [test]
strategy:
Expand Down
82 changes: 82 additions & 0 deletions tests/cli/track_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cli

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var integration = os.Getenv("INTEGRATION")

func skipIntegration(t *testing.T) {
t.Helper()
if integration != "1" {
t.Skip("Skipping integration tests")
}
}

func TestCLI(t *testing.T) {
skipIntegration(t)

tempDir, err := os.MkdirTemp("", "")
require.NoErrorf(t, err, "error creating temporary directory: %s", err)

binPath := filepath.Join(tempDir, "hours")
buildArgs := []string{"build", "-o", binPath, "../.."}

c := exec.Command("go", buildArgs...)
err = c.Run()
require.NoErrorf(t, err, "error building binary: %s", err)

defer func() {
err := os.RemoveAll(tempDir)
if err != nil {
fmt.Printf("couldn't clean up temporary directory (%s): %s", binPath, err)
}
}()

t.Run("TestHelp", func(t *testing.T) {
// GIVEN
// WHEN
c := exec.Command(binPath, "-h")
b, err := c.CombinedOutput()

// THEN
assert.NoError(t, err, "output:\n%s", b)
})

t.Run("TestGen", func(t *testing.T) {
// GIVEN
// WHEN
dbPath := filepath.Join(tempDir, "db.db")
c := exec.Command(binPath, "gen", "-y", "-d", dbPath)
b, err := c.CombinedOutput()

// THEN
assert.NoError(t, err, "output:\n%s", b)
})

t.Run("TestListTasks", func(t *testing.T) {
// GIVEN
dbPath := filepath.Join(tempDir, "db.db")
c := exec.Command(binPath, "gen", "-y", "-d", dbPath)
b, err := c.CombinedOutput()
require.NoError(t, err, "couldn't generate dummy data, output:\n\n%s", b)

// WHEN
lc := exec.Command(binPath, "tasks", "-l", "3", "-d", dbPath)
o, err := lc.CombinedOutput()

// THEN
require.NoError(t, err, "output:\n%s", o)
var js interface{}
err = json.Unmarshal(o, &js)
assert.NoError(t, err, "output is not valid json; output:\n%s", o)
})
}

0 comments on commit 759fe17

Please sign in to comment.