-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding more tests for commands; Setting up to properly show version o…
…n --version
- Loading branch information
1 parent
8c3e1a3
commit 2edb741
Showing
18 changed files
with
473 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(` | ||
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(` | ||
Stacks: | ||
test-01 | ||
`, | ||
)) | ||
|
||
if !strings.Contains(output, expectedContent) { | ||
t.Errorf("Expected output to contain '%s', but got: %s", expectedContent, output) | ||
} | ||
}, | ||
) | ||
} |
Oops, something went wrong.