8000 Ensure MAGEFILE_VERBOSE set in compiled magefile by axw · Pull Request #300 · magefile/mage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ensure MAGEFILE_VERBOSE set in compiled magefile #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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