-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
218 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package integration | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
var coverageEnv struct { | ||
coverdir string | ||
once sync.Once | ||
} | ||
|
||
// SetupCoverage sets up the given test environment for coverage | ||
func SetupCoverage(p *testscript.Params) error { | ||
coverdir := os.Getenv("GOCOVERDIR_TXTAR") | ||
if testing.CoverMode() == "" || coverdir == "" { | ||
return nil | ||
} | ||
|
||
var err error | ||
|
||
if !filepath.IsAbs(coverdir) { | ||
coverdir, err = filepath.Abs(coverdir) | ||
if err != nil { | ||
return fmt.Errorf("unable to determine absolute path of %q: %w", coverdir, err) | ||
} | ||
} | ||
|
||
info, err := os.Stat(coverdir) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
if mkErr := os.Mkdir(coverdir, 0o755); mkErr != nil { | ||
return fmt.Errorf("failed to testscripts coverage dir %q: %w", coverdir, mkErr) | ||
} | ||
} else { | ||
// Handle other potential errors from os.Stat | ||
return fmt.Errorf("failed to stat %q: %w", coverdir, err) | ||
} | ||
} else if !info.IsDir() { | ||
return fmt.Errorf("coverage: %q is not a directory", coverdir) | ||
} | ||
|
||
origSetup := p.Setup | ||
p.Setup = func(env *testscript.Env) error { | ||
if origSetup != nil { | ||
origSetup(env) | ||
} | ||
|
||
// override GOCOVEDIR directory | ||
env.Setenv("GOCOVERDIR", coverdir) | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package integration | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
var gnoEnv struct { | ||
err error | ||
gnoBin string | ||
once sync.Once | ||
} | ||
|
||
func SetupGno(p *testscript.Params, buildDir string) error { | ||
gnoroot := os.Getenv("GNOROOT") | ||
if gnoroot == "" { | ||
// Get root location of github.com/gnolang/gno | ||
goModPath, err := exec.Command("go", "env", "GOMOD").CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("unable to determine gno root directory") | ||
} | ||
|
||
gnoroot = filepath.Dir(string(goModPath)) | ||
} | ||
|
||
info, err := os.Stat(buildDir) | ||
if err != nil { | ||
return fmt.Errorf("unable to stat: %q", buildDir) | ||
} | ||
|
||
if !info.IsDir() { | ||
return fmt.Errorf("given buildDir is not a directory: %q", buildDir) | ||
} | ||
|
||
gnoBin := filepath.Join(buildDir, "gno") | ||
if _, err = os.Stat(gnoBin); errors.Is(err, os.ErrNotExist) { | ||
// Build a fresh gno binary in a temp directory | ||
gnoArgsBuilder := []string{"build", "-o", gnoBin} | ||
|
||
// Add coverage if needed | ||
if coverMode := testing.CoverMode(); coverMode != "" { | ||
gnoArgsBuilder = append(gnoArgsBuilder, "-covermode", coverMode) | ||
} | ||
|
||
// Add target command | ||
gnoArgsBuilder = append(gnoArgsBuilder, filepath.Join(gnoroot, "gnovm", "cmd", "gno")) | ||
|
||
if err = exec.Command("go", gnoArgsBuilder...).Run(); err != nil { | ||
return fmt.Errorf("uanble to build gno binary: %w", err) | ||
} | ||
} else if err != nil { | ||
return err | ||
|
||
} | ||
|
||
// Define setup scripts | ||
origSetup := p.Setup | ||
p.Setup = func(env *testscript.Env) error { | ||
if origSetup != nil { | ||
if err := origSetup(env); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
home, err := os.MkdirTemp("", "gno") | ||
if err != nil { | ||
return fmt.Errorf("unable to create temporary home directory: %w", err) | ||
} | ||
|
||
env.Vars = append(env.Vars, | ||
"GNOROOT="+gnoroot, // thx PR 1014 :) | ||
// by default, $HOME=/no-home, but we need an existing $HOME directory | ||
// because some commands needs to access $HOME/.cache/go-build | ||
"HOME="+home, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
if p.Cmds == nil { | ||
p.Cmds = make(map[string]func(ts *testscript.TestScript, neg bool, args []string)) | ||
} | ||
|
||
// Set gno command | ||
p.Cmds["gno"] = func(ts *testscript.TestScript, neg bool, args []string) { | ||
err := ts.Exec(gnoBin, args...) | ||
if err != nil { | ||
ts.Logf("[%v]\n", err) | ||
if !neg { | ||
ts.Fatalf("unexpected gno command failure") | ||
} | ||
} else { | ||
if neg { | ||
ts.Fatalf("unexpected gno command success") | ||
} | ||
} | ||
} | ||
return nil | ||
} |