Skip to content

Commit

Permalink
Fix failure without config and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
btromanova committed Oct 2, 2019
1 parent 54821f5 commit ec8314c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
14 changes: 9 additions & 5 deletions astro/cli/astro/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,10 @@ func (cli *AstroCLI) configureDynamicUserFlags() {

func (cli *AstroCLI) createRootCommand() {
rootCmd := &cobra.Command{
Use: "astro",
Short: "A tool for managing multiple Terraform modules.",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: cli.preRun,
Use: "astro",
Short: "A tool for managing multiple Terraform modules.",
SilenceUsage: true,
SilenceErrors: true,
}

rootCmd.PersistentFlags().BoolVarP(&cli.flags.verbose, "verbose", "v", false, "verbose output")
Expand All @@ -181,6 +180,7 @@ func (cli *AstroCLI) createApplyCmd() {
Use: "apply [flags] [-- [Terraform argument]...]",
DisableFlagsInUseLine: true,
Short: "Run Terraform apply on all modules",
PersistentPreRunE: cli.preRun,
RunE: cli.runApply,
}

Expand All @@ -194,6 +194,7 @@ func (cli *AstroCLI) createPlanCmd() {
Use: "plan [flags] [-- [Terraform argument]...]",
DisableFlagsInUseLine: true,
Short: "Generate execution plans for modules",
PersistentPreRunE: cli.preRun,
RunE: cli.runPlan,
}

Expand All @@ -206,6 +207,9 @@ func (cli *AstroCLI) createPlanCmd() {
func (cli *AstroCLI) preRun(cmd *cobra.Command, args []string) error {
logger.Trace.Println("cli: in preRun")

if cli.config == nil {
return fmt.Errorf("unable to find config file")
}
// Load astro from config
project, err := astro.NewProject(astro.WithConfig(*cli.config))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion astro/cli/astro/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (cli *AstroCLI) createVersionCmd() {
versionString = append(versionString, fmt.Sprintf("built %s", date))
}

println(strings.Join(versionString, " "))
fmt.Fprintln(cli.stdout, strings.Join(versionString, " "))

return nil
},
Expand Down
68 changes: 65 additions & 3 deletions astro/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ func stringVersionMatches(v string, versionConstraint string) bool {
}

// compiles the astro binary and returns the path to it.
func compileAstro(dir string) (string, error) {
func compileAstro(dir string, ldflags []string) (string, error) {
astroPath := filepath.Join(dir, "astro")
packageName := "github.com/uber/astro/astro/cli/astro"
out, err := exec.Command("go", "build", "-o", astroPath, packageName).CombinedOutput()
compileArgs := []string{"build"}
if len(ldflags) > 0 {
compileArgs = append(compileArgs, "-ldflags")
for _, ldflag := range ldflags {
compileArgs = append(compileArgs, ldflag)
}
}
compileArgs = append(compileArgs, "-o", astroPath, packageName)
out, err := exec.Command("go", compileArgs...).CombinedOutput()
if err != nil {
return "", errors.New(string(out))
}
Expand All @@ -95,7 +103,7 @@ func TestPlanInterrupted(t *testing.T) {
defer os.RemoveAll(tmpdir)
require.NoError(t, err)

astroBinary, err := compileAstro(tmpdir)
astroBinary, err := compileAstro(tmpdir, []string{})
require.NoError(t, err)
command := exec.Command(astroBinary, "plan")

Expand Down Expand Up @@ -219,3 +227,57 @@ func TestProjectPlanDetachSuccess(t *testing.T) {
})
}
}

func TestVersionDev(t *testing.T) {
dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
result := RunTest(t, []string{"version"}, "/tmp/astro-tests", "")
assert.Equal(t, "", result.Stderr.String())
assert.Equal(t, "astro version dev\n", result.Stdout.String())
assert.Equal(t, 0, result.ExitCode)
}

func TestVersionWithLdflags(t *testing.T) {
dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)

stdoutBytes := &bytes.Buffer{}
stderrBytes := &bytes.Buffer{}

astroBinary, err := compileAstro(dir, []string{
"-X github.com/uber/astro/astro/cli/astro/cmd.version=1.2.3 " +
"-X github.com/uber/astro/astro/cli/astro/cmd.commit=ab123 " +
"-X github.com/uber/astro/astro/cli/astro/cmd.date=2019-01-01T10:00:00",
})
require.NoError(t, err)
command := exec.Command(astroBinary, "version")
command.Dir = dir

command.Stdout = stdoutBytes
command.Stderr = stderrBytes

err = command.Run()

require.NoError(t, err)

assert.Equal(t, "", stderrBytes.String())
assert.Equal(t, "astro version 1.2.3 (ab123) built 2019-01-01T10:00:00\n", stdoutBytes.String())
}

func TestPlanErorrsWithoutConfig(t *testing.T) {
dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
result := RunTest(t, []string{"plan"}, "/tmp/astro-tests", "")
assert.Equal(t, "unable to find config file\n", result.Stderr.String())
assert.Equal(t, "", result.Stdout.String())
assert.Equal(t, 1, result.ExitCode)
}

0 comments on commit ec8314c

Please sign in to comment.