diff --git a/main.go b/main.go index 5e67da8..0594dd5 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ type config struct { // Arguments struct stores the arguments passed to cfdtunel such as the profile to use, the command to run and the arguments for that command type Arguments struct { - profile *string + profile string command string args []string } @@ -53,13 +53,13 @@ func main() { config, err := readIniConfigFile(getHomePathIniFile(iniConfigFile)) if err != nil { - log.Fatalf("An error occured reading your INI file: %v", err.Error()) + log.Fatalf("An error occurred reading your INI file: %v", err.Error()) } - tunnelConfig, err := config.readConfigSection(*args.profile) + tunnelConfig, err := config.readConfigSection(args.profile) if err != nil { - log.Fatalf("An error occured reading your INI file: %v", err.Error()) + log.Fatalf("An error occurred reading your INI file: %v", err.Error()) } tunnelConfig.setupEnvironmentVariables() cmd := tunnelConfig.startProxyTunnel() @@ -145,7 +145,7 @@ func (cfg config) readConfigSection(section string) (TunnelConfig, error) { secs, err := cfg.ini.GetSection(section) if err != nil { - log.Debugf("An error occured: %v", err.Error()) + log.Debugf("An error occurred: %v", err.Error()) return TunnelConfig{}, err } @@ -192,15 +192,14 @@ func flagArguments() Arguments { } if *profile == "" { - fmt.Println("Usage: cfdtunnel --profile xxx command args") + fmt.Println("Usage: cfdtunnel --profile my-profile command args") os.Exit(1) - return Arguments{} } args := flag.Args() return Arguments{ - profile: profile, + profile: *profile, command: args[0], args: args[1:], } @@ -210,7 +209,7 @@ func flagArguments() Arguments { func checkSubCommandExists(command string) bool { _, err := exec.LookPath(command) if err != nil { - log.Errorf("An error occured: %v", err.Error()) + log.Errorf("An error occurred: %v", err.Error()) return false } diff --git a/main_test.go b/main_test.go index 54cc70d..9e8ba2b 100644 --- a/main_test.go +++ b/main_test.go @@ -1,12 +1,14 @@ package main import ( - "log" + "io/ioutil" "os" "os/exec" "path" "testing" + "log" + "github.com/stretchr/testify/assert" "gopkg.in/ini.v1" ) @@ -130,7 +132,7 @@ func TestTunnelSamePort(t *testing.T) { func TestFlagsEmptyArguments(t *testing.T) { // Run the crashing code when FLAG is set if os.Getenv("FLAG") == "1" { - // Calls flagArguments withour manipulate os.Args + // Calls flagArguments without manipulate os.Args _ = flagArguments() return } @@ -142,11 +144,11 @@ func TestFlagsEmptyArguments(t *testing.T) { // Cast the error as *exec.ExitError and compare the result e, ok := err.(*exec.ExitError) - assert.Equal(t, true, ok) + assert.True(t, ok) assert.Equal(t, "exit status 1", e.Error()) } -func TestFlagArguments(t *testing.T) { +func TestFlagsArguments(t *testing.T) { oldArgs := os.Args defer func() { os.Args = oldArgs }() @@ -154,11 +156,33 @@ func TestFlagArguments(t *testing.T) { os.Args = []string{"rootTest", "--profile", "alias", "subcommand", "arg1", "arg2"} args := flagArguments() - assert.Equal(t, "alias", *args.profile) + assert.Equal(t, "alias", args.profile) assert.Equal(t, "subcommand", args.command) assert.Equal(t, []string{"arg1", "arg2"}, args.args) } +func TestFlagsProfileMissing(t *testing.T) { + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + + // Run the crashing code when FLAG is set + if os.Getenv("FLAG") == "1" { + os.Args = []string{"rootTest", "subcommand", "arg1", "arg2"} + _ = flagArguments() + return + } + + // Run the test in a subprocess + cmd := exec.Command(os.Args[0], "-test.run=TestFlagsProfileMissing") + cmd.Env = append(os.Environ(), "FLAG=1") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + e, ok := err.(*exec.ExitError) + assert.True(t, ok) + assert.Equal(t, "exit status 1", e.Error()) +} + func TestSubCommandExists(t *testing.T) { assert.True(t, checkSubCommandExists("echo")) @@ -173,3 +197,49 @@ func TestConfigSectionDoesNotExists(t *testing.T) { assert.Error(t, err) } + +func TestRunSubCommandStdOut(t *testing.T) { + args := Arguments{ + profile: "alias", + command: "ls", + args: []string{"README.md"}, + } + + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + args.runSubCommand() + + w.Close() + out, _ := ioutil.ReadAll(r) + os.Stdout = rescueStdout + + assert.Equal(t, "README.md\n\n", string(out)) +} + +func TestRunSubCommandMissing(t *testing.T) { + + // Run the crashing code when FLAG is set + if os.Getenv("FLAG") == "1" { + args := Arguments{ + profile: "alias", + command: "lsssss", + args: nil, + } + args.runSubCommand() + + return + } + + // Run the test in a subprocess + cmd := exec.Command(os.Args[0], "-test.run=TestRunSubCommandMissing") + cmd.Env = append(os.Environ(), "FLAG=1") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + e, ok := err.(*exec.ExitError) + assert.True(t, ok) + assert.Equal(t, "exit status 1", e.Error()) + +}