Skip to content

Commit

Permalink
Mock env in tests to simplify run on Travis CI
Browse files Browse the repository at this point in the history
  • Loading branch information
mingan committed Apr 15, 2018
1 parent 798e6c9 commit 2a05772
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
19 changes: 11 additions & 8 deletions cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
Expand All @@ -12,10 +13,10 @@ import (
"text/tabwriter"
"time"

"github.com/adammck/venv"
"github.com/mikkeloscar/sshconfig"
"github.com/pkg/errors"
"github.com/pressly/sup"
"io"
)

var (
Expand Down Expand Up @@ -52,6 +53,7 @@ type options struct {
exceptHosts string
debug bool
disablePrefix bool
env venv.Env
}

func init() {
Expand Down Expand Up @@ -85,6 +87,7 @@ func main() {
return
}

flags.env = venv.OS()
if err := runSupfile(os.Stderr, flags, flag.Args()); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -99,7 +102,7 @@ func runSupfile(errStream io.Writer, options options, args []string) error {
return err
}
// Parse network and commands to be run from flags.
network, commands, err := parseArgs(errStream, args, conf)
network, commands, err := parseArgs(errStream, options, args, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -247,7 +250,7 @@ func resolvePath(path string) string {

// parseArgs parses args and returns network and commands to be run.
// On error, it prints usage and exits.
func parseArgs(errStream io.Writer, args []string, conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
func parseArgs(errStream io.Writer, options options, args []string, conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
var commands []*sup.Command

if len(args) < 1 {
Expand Down Expand Up @@ -290,15 +293,15 @@ func parseArgs(errStream io.Writer, args []string, conf *sup.Supfile) (*sup.Netw

// Add default nonce
network.Env.Set("SUP_TIME", time.Now().UTC().Format(time.RFC3339))
if os.Getenv("SUP_TIME") != "" {
network.Env.Set("SUP_TIME", os.Getenv("SUP_TIME"))
if options.env.Getenv("SUP_TIME") != "" {
network.Env.Set("SUP_TIME", options.env.Getenv("SUP_TIME"))
}

// Add user
if os.Getenv("SUP_USER") != "" {
network.Env.Set("SUP_USER", os.Getenv("SUP_USER"))
if options.env.Getenv("SUP_USER") != "" {
network.Env.Set("SUP_USER", options.env.Getenv("SUP_USER"))
} else {
network.Env.Set("SUP_USER", os.Getenv("USER"))
network.Env.Set("SUP_USER", options.env.Getenv("USER"))
}

for _, cmd := range args[1:] {
Expand Down
34 changes: 26 additions & 8 deletions cmd/sup/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"testing"
"time"

"github.com/adammck/venv"
)

const (
envTestUser = "sup_test_user"
)

var (
Expand All @@ -28,6 +33,7 @@ we kp re
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -55,6 +61,7 @@ commands:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{}); err != ErrUsage {
t.Fatal(err)
Expand Down Expand Up @@ -107,6 +114,7 @@ commands:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{"staging"}); err != ErrNetworkNoHosts {
t.Fatal(err)
Expand Down Expand Up @@ -134,6 +142,7 @@ commands:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{"staging"}); err != ErrUsage {
t.Fatal(err)
Expand Down Expand Up @@ -168,6 +177,7 @@ targets:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "step5"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -205,6 +215,7 @@ targets:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "walk"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -389,6 +400,7 @@ commands:
options := options{
dirname: dirname,
onlyHosts: "server42",
env: venv.Mock(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -420,6 +432,7 @@ commands:
options := options{
dirname: dirname,
onlyHosts: "server(",
env: venv.Mock(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -487,6 +500,7 @@ commands:
options := options{
dirname: dirname,
exceptHosts: "server",
env: venv.Mock(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -518,6 +532,7 @@ commands:
options := options{
dirname: dirname,
exceptHosts: "server(",
env: venv.Mock(),
}
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
t.Fatal("Expected an error")
Expand Down Expand Up @@ -578,6 +593,7 @@ commands:
withTmpDir(t, input, func(dirname string) {
options := options{
dirname: dirname,
env: testEnv(),
}
args := []string{"staging", "step1"}
if err := runSupfile(testErrStream, options, args); err == nil {
Expand Down Expand Up @@ -618,15 +634,11 @@ commands:
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
t.Fatal(err)
}
currentUser, err := user.Current()
if err != nil {
t.Fatal(err)
}
m := newMatcher(outputs, t)
m.expectActivityOnServers(0, 1)
m.expectExportOnActiveServers(`SUP_NETWORK="staging"`)
m.expectExportOnActiveServers(`SUP_ENV=""`)
m.expectExportOnActiveServers(fmt.Sprintf(`SUP_USER="%s"`, currentUser.Name))
m.expectExportOnActiveServers(fmt.Sprintf(`SUP_USER="%s"`, envTestUser))
m.expectExportRegexpOnActiveServers(`SUP_HOST="localhost:\d+"`)
})
})
Expand Down Expand Up @@ -694,7 +706,7 @@ commands:
if err != nil {
t.Fatal(err)
}
os.Setenv("SUP_TIME", "now")
options.env.Setenv("SUP_TIME", "now")

if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -726,7 +738,7 @@ commands:
if err != nil {
t.Fatal(err)
}
os.Setenv("SUP_USER", "sup_rules")
options.env.Setenv("SUP_USER", "sup_rules")

if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1109,3 +1121,9 @@ func writeSupfileAs(dirname, filename, input string) error {
0666,
)
}

func testEnv() venv.Env {
env := venv.Mock()
env.Setenv("USER", envTestUser)
return env
}
1 change: 1 addition & 0 deletions cmd/sup/mock_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func setupMockEnv(dirname string, count int) ([]bytes.Buffer, options, error) {
options := options{
sshConfig: sshConfigPath,
dirname: dirname,
env: testEnv(),
}
return outputs, options, nil
}
Expand Down

0 comments on commit 2a05772

Please sign in to comment.