Skip to content

Commit cb825cb

Browse files
authored
Ensure MAGEFILE_VERBOSE set in compiled magefile (#300)
* 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.
1 parent 7747d7c commit cb825cb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

mage/main_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,69 @@ func TestCompiledEnvironmentVars(t *testing.T) {
11431143
}
11441144
}
11451145

1146+
func TestCompiledVerboseFlag(t *testing.T) {
1147+
stderr := &bytes.Buffer{}
1148+
stdout := &bytes.Buffer{}
1149+
dir := "./testdata/compiled"
1150+
compileDir, err := ioutil.TempDir(dir, "")
1151+
if err != nil {
1152+
t.Fatal(err)
1153+
}
1154+
filename := filepath.Join(compileDir, "mage_out")
1155+
// The CompileOut directory is relative to the
1156+
// invocation directory, so chop off the invocation dir.
1157+
outName := "./" + filename[len(dir)-1:]
1158+
defer os.RemoveAll(compileDir)
1159+
inv := Invocation{
1160+
Dir: dir,
1161+
Stdout: stdout,
1162+
Stderr: stderr,
1163+
CompileOut: outName,
1164+
}
1165+
code := Invoke(inv)
1166+
if code != 0 {
1167+
t.Errorf("expected to exit with code 0, but got %v, stderr: %s", code, stderr)
1168+
}
1169+
1170+
run := func(verboseEnv string, args ...string) string {
1171+
var stdout, stderr bytes.Buffer
1172+
args = append(args, "printverboseflag")
1173+
cmd := exec.Command(filename, args...)
1174+
cmd.Env = []string{verboseEnv}
1175+
cmd.Stderr = &stderr
1176+
cmd.Stdout = &stdout
1177+
if err := cmd.Run(); err != nil {
1178+
t.Fatalf("running '%s %s' with env %s failed with: %v\nstdout: %s\nstderr: %s",
1179+
filename, strings.Join(args, " "), verboseEnv, err, stdout.String(), stderr.String())
1180+
}
1181+
return strings.TrimSpace(stdout.String())
1182+
}
1183+
1184+
got := run("MAGEFILE_VERBOSE=false")
1185+
want := "mg.Verbose()==false"
1186+
if got != want {
1187+
t.Errorf("got %q, expected %q", got, want)
1188+
}
1189+
1190+
got = run("MAGEFILE_VERBOSE=false", "-v")
1191+
want = "mg.Verbose()==true"
1192+
if got != want {
1193+
t.Errorf("got %q, expected %q", got, want)
1194+
}
1195+
1196+
got = run("MAGEFILE_VERBOSE=true")
1197+
want = "mg.Verbose()==true"
1198+
if got != want {
1199+
t.Errorf("got %q, expected %q", got, want)
1200+
}
1201+
1202+
got = run("MAGEFILE_VERBOSE=true", "-v=false")
1203+
want = "mg.Verbose()==false"
1204+
if got != want {
1205+
t.Errorf("got %q, expected %q", got, want)
1206+
}
1207+
}
1208+
11461209
func TestClean(t *testing.T) {
11471210
if err := os.RemoveAll(mg.CacheDir()); err != nil {
11481211
t.Error("error removing cache dir:", err)

mage/template.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ Options:
186186
}
187187
_ = handleError
188188
189+
// Set MAGEFILE_VERBOSE so mg.Verbose() reflects the flag value.
190+
if args.Verbose {
191+
os.Setenv("MAGEFILE_VERBOSE", "1")
192+
} else {
193+
os.Setenv("MAGEFILE_VERBOSE", "0")
194+
}
195+
189196
log.SetFlags(0)
190197
if !args.Verbose {
191198
log.SetOutput(ioutil.Discard)

mage/testdata/compiled/custom.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"fmt"
78
"log"
89
"time"
910

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

21+
// PrintVerboseFlag prints the value of mg.Verbose() to stdout.
22+
func PrintVerboseFlag() {
23+
fmt.Printf("mg.Verbose()==%v", mg.Verbose())
24+
}
25+
2026
// This is the synopsis for Deploy. This part shouldn't show up.
2127
func Deploy() {
2228
mg.Deps(f)

0 commit comments

Comments
 (0)