Skip to content

Commit

Permalink
Ensure MAGEFILE_VERBOSE set in compiled magefile (#300)
Browse files Browse the repository at this point in the history
* Ensure MAGEFILE_VERBOSE set in compiled magefile

When using `mage -compile=foo`, ensure that passing
`-v` to the produced binary sets MAGEFILE_VERBOSE.
This is necessary for `mg.Verbose()` to return the
correct value.
  • Loading branch information
axw authored Apr 16, 2020
1 parent 7747d7c commit cb825cb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
63 changes: 63 additions & 0 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,69 @@ func TestCompiledEnvironmentVars(t *testing.T) {
}
}

func TestCompiledVerboseFlag(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
dir := "./testdata/compiled"
compileDir, err := ioutil.TempDir(dir, "")
if err != nil {
t.Fatal(err)
}
filename := filepath.Join(compileDir, "mage_out")
// The CompileOut directory is relative to the
// invocation directory, so chop off the invocation dir.
outName := "./" + filename[len(dir)-1:]
defer os.RemoveAll(compileDir)
inv := Invocation{
Dir: dir,
Stdout: stdout,
Stderr: stderr,
CompileOut: outName,
}
code := Invoke(inv)
if code != 0 {
t.Errorf("expected to exit with code 0, but got %v, stderr: %s", code, stderr)
}

run := func(verboseEnv string, args ...string) string {
var stdout, stderr bytes.Buffer
args = append(args, "printverboseflag")
cmd := exec.Command(filename, args...)
cmd.Env = []string{verboseEnv}
cmd.Stderr = &stderr
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
t.Fatalf("running '%s %s' with env %s failed with: %v\nstdout: %s\nstderr: %s",
filename, strings.Join(args, " "), verboseEnv, err, stdout.String(), stderr.String())
}
return strings.TrimSpace(stdout.String())
}

got := run("MAGEFILE_VERBOSE=false")
want := "mg.Verbose()==false"
if got != want {
t.Errorf("got %q, expected %q", got, want)
}

got = run("MAGEFILE_VERBOSE=false", "-v")
want = "mg.Verbose()==true"
if got != want {
t.Errorf("got %q, expected %q", got, want)
}

got = run("MAGEFILE_VERBOSE=true")
want = "mg.Verbose()==true"
if got != want {
t.Errorf("got %q, expected %q", got, want)
}

got = run("MAGEFILE_VERBOSE=true", "-v=false")
want = "mg.Verbose()==false"
if got != want {
t.Errorf("got %q, expected %q", got, want)
}
}

func TestClean(t *testing.T) {
if err := os.RemoveAll(mg.CacheDir()); err != nil {
t.Error("error removing cache dir:", err)
Expand Down
7 changes: 7 additions & 0 deletions mage/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ Options:
}
_ = handleError
// Set MAGEFILE_VERBOSE so mg.Verbose() reflects the flag value.
if args.Verbose {
os.Setenv("MAGEFILE_VERBOSE", "1")
} else {
os.Setenv("MAGEFILE_VERBOSE", "0")
}
log.SetFlags(0)
if !args.Verbose {
log.SetOutput(ioutil.Discard)
Expand Down
6 changes: 6 additions & 0 deletions mage/testdata/compiled/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"fmt"
"log"
"time"

Expand All @@ -17,6 +18,11 @@ func TestVerbose() {
log.Println("hi!")
}

// PrintVerboseFlag prints the value of mg.Verbose() to stdout.
func PrintVerboseFlag() {
fmt.Printf("mg.Verbose()==%v", mg.Verbose())
}

// This is the synopsis for Deploy. This part shouldn't show up.
func Deploy() {
mg.Deps(f)
Expand Down

0 comments on commit cb825cb

Please sign in to comment.