Skip to content

Commit

Permalink
fix: make coverage works
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Dec 29, 2024
1 parent c2064c0 commit 54a776b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 62 deletions.
23 changes: 19 additions & 4 deletions gno.land/pkg/integration/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/signal"
"slices"
"sync"
"testing"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
Expand All @@ -27,6 +28,8 @@ import (
"github.com/stretchr/testify/require"
)

const gracefulShutdown = time.Second * 5

type ProcessNodeConfig struct {
ValidatorKey ed25519.PrivKeyEd25519 `json:"priv"`
Verbose bool `json:"verbose"`
Expand All @@ -40,6 +43,7 @@ type ProcessConfig struct {
Node *ProcessNodeConfig

// These parameters are not meant to be passed to the process
CoverDir string
Stderr, Stdout io.Writer
}

Expand Down Expand Up @@ -187,6 +191,10 @@ func RunNodeProcess(ctx context.Context, cfg ProcessConfig, name string, args ..
cmd.Env = os.Environ()
cmd.Stdin = bytes.NewReader(nodeConfigData)

if cfg.CoverDir != "" {
cmd.Env = append(cmd.Env, "GOCOVERDIR="+cfg.CoverDir)
}

// Redirect all errors into a buffer
cmd.Stderr = os.Stderr
if cfg.Stderr != nil {
Expand Down Expand Up @@ -306,9 +314,8 @@ func RunMain(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) err

// Attempt graceful shutdown
select {
case <-ctx.Done():
err = ctx.Err()
// log.Fatalf("unable to gracefully stop the node, exiting now")
case <-time.After(gracefulShutdown):
return fmt.Errorf("unable to gracefully stop the node, exiting now")

Check warning on line 318 in gno.land/pkg/integration/process.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/integration/process.go#L317-L318

Added lines #L317 - L318 were not covered by tests
case err = <-ccErr: // done
}

Expand All @@ -318,8 +325,16 @@ func RunMain(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) err
func runTestingNodeProcess(t TestingTS, ctx context.Context, pcfg ProcessConfig) NodeProcess {
bin, err := os.Executable()
require.NoError(t, err)
args := []string{
"-test.run=^$",
"-run-node-process",
}

if pcfg.CoverDir != "" && testing.CoverMode() != "" {
args = append(args, "-test.gocoverdir="+pcfg.CoverDir)
}

node, err := RunNodeProcess(ctx, pcfg, bin, "-test.run=^$", "-run-node-process")
node, err := RunNodeProcess(ctx, pcfg, bin, args...)
require.NoError(t, err)

return node
Expand Down
61 changes: 6 additions & 55 deletions gno.land/pkg/integration/process/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,19 @@ package main

import (
"context"
"encoding/json"
"io"
"log"
"fmt"
"os"
"os/signal"
"runtime/pprof"
"time"

"github.com/gnolang/gno/gno.land/pkg/integration"
)

const gracefulShutdown = time.Second * 5

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

// Get the CPU profile path from the environment variable
if profilePath := os.Getenv("CPUPROFILE_PATH"); profilePath != "" {
// Create a file to store the CPU profile
cpuProfile, err := os.Create(profilePath)
if err != nil {
log.Fatalf("could not create CPU profile: %v", err)
}
defer cpuProfile.Close()

// Start CPU profiling
if err := pprof.StartCPUProfile(cpuProfile); err != nil {
log.Fatalf("could not start CPU profile: %v", err)
}
defer pprof.StopCPUProfile() // Ensure the profile is stopped when the program exits
}

// Read the configuration from standard input
configData, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading stdin: %v", err)
}

// Unmarshal the JSON configuration
var cfg integration.ProcessNodeConfig
if err := json.Unmarshal(configData, &cfg); err != nil {
log.Fatalf("error unmarshaling JSON: %v", err)
}

// Run the node
ccErr := make(chan error, 1)
go func() {
ccErr <- integration.RunNode(ctx, &cfg, os.Stdout, os.Stderr)
close(ccErr)
}()

// Wait for the node to gracefully terminate
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

// Attempt graceful shutdown
select {
case <-time.After(gracefulShutdown):
log.Fatalf("unable to gracefully stop the node, exiting now")
case err := <-ccErr: // done
if err != nil {
log.Fatalf("unable to gracefully stop the node: %v", err)
}
if err := integration.RunMain(ctx, os.Stdin, os.Stdout, os.Stderr); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
4 changes: 1 addition & 3 deletions gno.land/pkg/integration/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/stretchr/testify/require"
)

const gracefulShutdown = time.Second * 5

// Define a flag to indicate whether to run the embedded command
var runCommand = flag.Bool("run-node-process", false, "execute the embedded command")

Expand All @@ -31,7 +29,7 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

ctx, cancel := context.WithTimeout(context.Background(), gracefulShutdown)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

if err := RunMain(ctx, os.Stdin, os.Stdout, os.Stderr); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions gno.land/pkg/integration/testscript_gnoland.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ func setupNode(ts *testscript.TestScript, ctx context.Context, cfg *ProcessNodeC
Stderr: ts.Stderr(),
}

// Setup coverdir provided
if coverdir := ts.Getenv("GOCOVERDIR"); coverdir != "" {
pcfg.CoverDir = coverdir
}

val := ts.Value(envKeyExecCommand)

switch cmd := val.(commandkind); cmd {
Expand Down

0 comments on commit 54a776b

Please sign in to comment.