Skip to content

Commit

Permalink
Improve test coverage (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiranda authored Nov 26, 2021
1 parent f9fce57 commit b065ce0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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:],
}
Expand All @@ -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
}

Expand Down
80 changes: 75 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -142,23 +144,45 @@ 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 }()

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"))
Expand All @@ -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())

}

0 comments on commit b065ce0

Please sign in to comment.