Skip to content

Commit

Permalink
Adding more tests for commands; Setting up to properly show version o…
Browse files Browse the repository at this point in the history
…n --version
  • Loading branch information
Shackelford-Arden committed Jul 16, 2024
1 parent 8c3e1a3 commit 2edb741
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240716-161044.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Ensuring we include the version during builds
time: 2024-07-16T16:10:44.764349-05:00
custom:
Author: Shackelford-Arden
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240716-161123.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Adding more testing for commands
time: 2024-07-16T16:11:23.031283-05:00
custom:
Author: Shackelford-Arden
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: stable
# It all depends on your needs.
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
uses: goreleaser/goreleaser-action@v6
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
Expand Down
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 1
version: 2

before:
hooks:
Expand All @@ -20,6 +20,8 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s -w -X version.Version={{.Version}} -X version.Commit={{.Commit}} -X version.Date={{.Date}} -X version.BuiltWith=goreleaser

archives:
- format: tar.gz
Expand Down
6 changes: 6 additions & 0 deletions build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package build

var Version = "0.0.0"
var Commit = ""
var Date = ""
var BuiltWith = ""
21 changes: 15 additions & 6 deletions cmd/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package cmd

import (
"fmt"
"github.com/urfave/cli/v2"
"os"

"github.com/urfave/cli/v2"
)

// Activate provides the given shell with the commands to set environment variables
// The intent is that this is sourced/imported in your shell, and not called ad-hoc.
func Activate(ctx *cli.Context) error {

execPath, _ := os.Executable()
activateScript := ""

shell := ctx.String("shell")
if shell == "" {
shell = AppConfig.Shell
}

// This bash/zsh script is pretty much the same as what mise has
// Reference: https://github.com/jdx/mise/blob/be34b768d9c09feda3c59d9a949a40609c294dcf/src/shell/zsh.rs#L17
activateScript := fmt.Sprintf(
`
switch shell {
default:
// This bash/zsh script is pretty much the same as what mise has
// Reference: https://github.com/jdx/mise/blob/be34b768d9c09feda3c59d9a949a40609c294dcf/src/shell/zsh.rs#L17
activateScript = fmt.Sprintf(`
hctx () {
local command
HCTX_PATH='%s'
Expand All @@ -36,7 +44,8 @@ hctx () {
command $HCTX_PATH "$command" "$@"
}
`, execPath,
)
)
}

fmt.Print(activateScript)

Expand Down
94 changes: 94 additions & 0 deletions cmd/activate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cmd

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
)

func TestActivateOutput(t *testing.T) {
app, _ := App()

t.Run("TestBashOutput", func(t *testing.T) {

execPath, _ := os.Executable()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Could not create pipe: %v", err)
}

// Save the original stdout and stderr
oldStdout := os.Stdout
oldStderr := os.Stderr

// Set the pipe's writer as stdout and stderr
os.Stdout = w
os.Stderr = w

// Create a channel to signal when we're done reading the output
done := make(chan bool)
var output string

// Start a goroutine to read from the pipe
// This allows us to capture the output of the CLI
// being called in the next section.
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output = buf.String()
done <- true
}()

args := []string{"hctx", "--shell", "bash", "activate"}
err = app.Run(args)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Restore the original stdout and stderr
os.Stdout = oldStdout
os.Stderr = oldStderr

// Close the writer side of the pipe
w.Close()

// Wait for the reading goroutine to finish
<-done

// Check for command execution error
if err != nil {
t.Errorf("Command execution failed: %v", err)
}

expectedContent := strings.TrimSpace(fmt.Sprintf(`
hctx () {
local command
HCTX_PATH='%s'
command="${1:-}"
if [ "$#" = 0 ]
then
command $HCTX_PATH
return
fi
shift
case "$command" in
(use|u|unset|un) if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]
then
eval "$(command $HCTX_PATH "$command" "$@")"
return $?
fi ;;
esac
command $HCTX_PATH "$command" "$@"
}
`, execPath,
))

if !strings.Contains(output, expectedContent) {
t.Errorf("Expected output to contain '%s', but got: %s", expectedContent, output)
}
},
)
}
4 changes: 2 additions & 2 deletions cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func ClearCache(ctx *cli.Context) error {
// ShowCache shows the content of the cache file.
func ShowCache(ctx *cli.Context) error {

cache, _ := AppCache.Get()
fmtCache, fmtErr := json.MarshalIndent(cache, "", " ")
currentCache, _ := AppCache.Get()
fmtCache, fmtErr := json.MarshalIndent(currentCache, "", " ")
if fmtErr != nil {
return fmt.Errorf("failed to read/format the cache: %s", fmtErr.Error())
}
Expand Down
1 change: 1 addition & 0 deletions cmd/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cmd
3 changes: 2 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"fmt"
"github.com/Shackelford-Arden/hctx/types"
"os"

"github.com/Shackelford-Arden/hctx/types"

"github.com/urfave/cli/v2"
)

Expand Down
167 changes: 167 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package cmd

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/Shackelford-Arden/hctx/types"
)

func TestListOutput(t *testing.T) {
app, _ := App()

testTmpDir := t.TempDir()

t.Run("TestIndicatorIncluded", func(t *testing.T) {

tmpConfig, _ := os.CreateTemp(testTmpDir, "indicator-*.hcl")
defer tmpConfig.Close()

tmpConfig.WriteString(`
stack "test-01" {
nomad {
address = "http://localhost:4646"
}
}
`)

r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Could not create pipe: %v", err)
}

// Save the original stdout and stderr
oldStdout := os.Stdout
oldStderr := os.Stderr

// Set the pipe's writer as stdout and stderr
os.Stdout = w
os.Stderr = w

// Create a channel to signal when we're done reading the output
done := make(chan bool)
var output string

// Start a goroutine to read from the pipe
// This allows us to capture the output of the CLI
// being called in the next section.
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output = buf.String()
done <- true
}()

// Set the environment variable to the stack name
t.Setenv(types.StackNameEnv, "test-01")

args := []string{"hctx", "--config", tmpConfig.Name(), "list"}
err = app.Run(args)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Restore the original stdout and stderr
os.Stdout = oldStdout
os.Stderr = oldStderr

// Close the writer side of the pipe
w.Close()

// Wait for the reading goroutine to finish
<-done

// Check for command execution error
if err != nil {
t.Errorf("Command execution failed: %v", err)
}

expectedContent := strings.TrimSpace(fmt.Sprintf(`

Check failure on line 83 in cmd/list_test.go

View workflow job for this annotation

GitHub Actions / check

unnecessary use of fmt.Sprintf (S1039)
Stacks:
test-01 *
`,
))

if !strings.Contains(output, expectedContent) {
t.Errorf("Expected output to contain '%s', but got: %s", expectedContent, output)
}
},
)

t.Run("TestNoStackSelected", func(t *testing.T) {

tmpConfig, _ := os.CreateTemp(testTmpDir, "indicator-*.hcl")
defer tmpConfig.Close()

tmpConfig.WriteString(`
stack "test-01" {
nomad {
address = "http://localhost:4646"
}
}
`)

r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Could not create pipe: %v", err)
}

// Save the original stdout and stderr
oldStdout := os.Stdout
oldStderr := os.Stderr

// Set the pipe's writer as stdout and stderr
os.Stdout = w
os.Stderr = w

// Create a channel to signal when we're done reading the output
done := make(chan bool)
var output string

// Start a goroutine to read from the pipe
// This allows us to capture the output of the CLI
// being called in the next section.
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
output = buf.String()
done <- true
}()

args := []string{"hctx", "--config", tmpConfig.Name(), "list"}
err = app.Run(args)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Restore the original stdout and stderr
os.Stdout = oldStdout
os.Stderr = oldStderr

// Close the writer side of the pipe
w.Close()

// Wait for the reading goroutine to finish
<-done

// Check for command execution error
if err != nil {
t.Errorf("Command execution failed: %v", err)
}

expectedContent := strings.TrimSpace(fmt.Sprintf(`

Check failure on line 156 in cmd/list_test.go

View workflow job for this annotation

GitHub Actions / check

unnecessary use of fmt.Sprintf (S1039)
Stacks:
test-01
`,
))

if !strings.Contains(output, expectedContent) {
t.Errorf("Expected output to contain '%s', but got: %s", expectedContent, output)
}
},
)
}
Loading

0 comments on commit 2edb741

Please sign in to comment.