Skip to content

Commit

Permalink
fix: Check if project is also empty before using an environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Jun 30, 2022
1 parent ec10325 commit 5d4e548
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c DefaultCommander) Exec(cmd *exec.Cmd) (string, error) {
output, err := cmd.CombinedOutput()
if err != nil {
if c.logger != nil {
c.logger.Println(err)
c.logger.Println(err, string(output))
}
return "", &ShellError{strings.Join(cmd.Args, " "), err}
}
Expand Down
12 changes: 6 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ func ParseOptions(argv []string) (*Options, error) {
return nil, err
}

// If config file flag is not set, and env is, use the env
val, ok := os.LookupEnv("SMUG_SESSION_CONFIG_PATH")
if *config == "" && ok {
*config = val
}

var project string
if *config == "" {
if errors.Is(cmdErr, ErrCommandNotFound) {
Expand All @@ -151,6 +145,12 @@ func ParseOptions(argv []string) (*Options, error) {
}
}

// If config file flag is not set, and env is, use the env
val, ok := os.LookupEnv("SMUG_SESSION_CONFIG_PATH")
if *config == "" && project == "" && ok {
*config = val
}

if strings.Contains(project, ":") {
parts := strings.Split(project, ":")
project = parts[0]
Expand Down
35 changes: 35 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"os"
"reflect"
"testing"
)
Expand All @@ -10,6 +11,7 @@ var usageTestTable = []struct {
argv []string
opts Options
err error
env map[string]string
}{
{
[]string{"start", "smug"},
Expand All @@ -24,6 +26,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "smug", "-w", "foo"},
Expand All @@ -38,6 +41,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "smug:foo,bar"},
Expand All @@ -52,6 +56,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "smug", "--attach", "--debug", "--detach"},
Expand All @@ -66,6 +71,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "smug", "-ad"},
Expand All @@ -80,6 +86,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "-f", "test.yml"},
Expand All @@ -94,6 +101,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2"},
Expand All @@ -108,6 +116,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"start", "project", "a=b", "x=y"},
Expand All @@ -125,6 +134,7 @@ var usageTestTable = []struct {
},
},
nil,
nil,
},
{
[]string{"start", "-f", "test.yml", "a=b", "x=y"},
Expand All @@ -142,6 +152,7 @@ var usageTestTable = []struct {
},
},
nil,
nil,
},
{
[]string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2", "a=b", "x=y"},
Expand All @@ -159,11 +170,13 @@ var usageTestTable = []struct {
},
},
nil,
nil,
},
{
[]string{"start", "--help"},
Options{},
ErrHelp,
nil,
},
{
[]string{"test"},
Expand All @@ -174,6 +187,7 @@ var usageTestTable = []struct {
Settings: map[string]string{},
},
nil,
nil,
},
{
[]string{"test", "-w", "win1", "-w", "win2", "a=b", "x=y"},
Expand All @@ -184,26 +198,47 @@ var usageTestTable = []struct {
Settings: map[string]string{"a": "b", "x": "y"},
},
nil,
nil,
},
{
[]string{"test"},
Options{
Command: "start",
Project: "test",
Config: "",
Windows: []string{},
Settings: map[string]string{},
},
nil,
map[string]string{
"SMUG_SESSION_CONFIG_PATH": "test",
},
},
{
[]string{},
Options{},
ErrHelp,
nil,
},
{
[]string{"--help"},
Options{},
ErrHelp,
nil,
},
{
[]string{"start", "--test"},
Options{},
errors.New("unknown flag: --test"),
nil,
},
}

func TestParseOptions(t *testing.T) {
for _, v := range usageTestTable {
for k, v := range v.env {
os.Setenv(k, v)
}
opts, err := ParseOptions(v.argv)
if v.err != nil && err != nil && err.Error() != v.err.Error() {
t.Errorf("expected error %v, got %v", v.err, err)
Expand Down

0 comments on commit 5d4e548

Please sign in to comment.