From 4827c98160552362c968407e69787976f33f2280 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Mon, 2 Dec 2024 19:56:58 +0100 Subject: [PATCH] Clean up tests and change revive formatter (#711) Co-authored-by: Sebastian (Tiedtke) Huckleberry --- Makefile | 2 +- internal/command/command_terminal_test.go | 2 +- .../projectservice/project_service_test.go | 35 --------------- internal/testutils/env.go | 4 +- main_permissions_test.go | 43 +------------------ 5 files changed, 6 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index 17551ffae..c72bad4c4 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ lint: @gofumpt -d . @revive \ -config revive.toml \ - -formatter stylish \ + -formatter friendly \ -exclude integration/subject/... \ ./... @staticcheck ./... diff --git a/internal/command/command_terminal_test.go b/internal/command/command_terminal_test.go index c50535c2d..e488119c3 100644 --- a/internal/command/command_terminal_test.go +++ b/internal/command/command_terminal_test.go @@ -64,7 +64,7 @@ func TestTerminalCommand_EnvPropagation(t *testing.T) { // Wait for the prompt before sending the next command. prompt := "$" - if testutils.IsDockerTestEnv() { + if testutils.IsRunningInDocker() { prompt = "#" } expectContainLines(ctx, t, stdout, []string{prompt}) diff --git a/internal/project/projectservice/project_service_test.go b/internal/project/projectservice/project_service_test.go index 8d6f64f5d..983cf29e6 100644 --- a/internal/project/projectservice/project_service_test.go +++ b/internal/project/projectservice/project_service_test.go @@ -11,8 +11,6 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "google.golang.org/grpc/test/bufconn" "github.com/stateful/runme/v3/internal/project/projectservice" @@ -78,39 +76,6 @@ func TestProjectServiceServer_Load(t *testing.T) { }) } -func TestProjectServiceServer_Load_ClientConnClosed(t *testing.T) { - t.Parallel() - - temp := t.TempDir() - testData := teststub.Setup(t, temp) - - lis, stop := testStartProjectServiceServer(t) - t.Cleanup(stop) - - clientConn, client := testutils.NewGRPCClientWithT(t, lis, projectv1.NewProjectServiceClient) - - req := &projectv1.LoadRequest{ - Kind: &projectv1.LoadRequest_File{ - File: &projectv1.FileProjectOptions{ - Path: testData.ProjectFilePath(), - }, - }, - } - loadClient, err := client.Load(context.Background(), req) - require.NoError(t, err) - - err = clientConn.Close() - require.NoError(t, err) - - for { - _, err := loadClient.Recv() - if err != nil { - require.Equal(t, codes.Canceled, status.Code(err)) - break - } - } -} - func collectLoadEventTypes(client projectv1.ProjectService_LoadClient) ([]projectv1.LoadEventType, error) { var eventTypes []projectv1.LoadEventType diff --git a/internal/testutils/env.go b/internal/testutils/env.go index 0d5310e4a..7538be5b7 100644 --- a/internal/testutils/env.go +++ b/internal/testutils/env.go @@ -2,9 +2,9 @@ package testutils import "os" -// IsDockerTestEnv returns true if the test is running in a Docker environment. +// IsRunningInDocker returns true if the test is running in a Docker environment. // Check out the Makefile's "test-docker/run" target where "RUNME_TEST_ENV" // is set to "docker". -func IsDockerTestEnv() bool { +func IsRunningInDocker() bool { return os.Getenv("RUNME_TEST_ENV") == "docker" } diff --git a/main_permissions_test.go b/main_permissions_test.go index 562840cba..ebbf5793e 100644 --- a/main_permissions_test.go +++ b/main_permissions_test.go @@ -3,54 +3,15 @@ package main import ( - "bufio" - "os" - "strings" "testing" "github.com/rogpeppe/go-internal/testscript" + "github.com/stateful/runme/v3/internal/testutils" ) -func isDocker() bool { - if _, err := os.Stat("/.dockerenv"); err == nil { - return true - } - - paths := []string{"/proc/1/cgroup", "/proc/self/cgroup"} - for _, path := range paths { - file, err := os.Open(path) - if err != nil { - continue - } - - scanner := bufio.NewScanner(file) - isDocker := false - for scanner.Scan() { - if strings.Contains(scanner.Text(), "docker") || strings.Contains(scanner.Text(), "kubepods") { - isDocker = true - break - } - } - - if err := scanner.Err(); err != nil { - _ = file.Close() - return false - } - - _ = file.Close() - - if isDocker { - return true - } - } - - return false -} - func TestRunmeFilePermissions(t *testing.T) { - if isDocker() { + if testutils.IsRunningInDocker() { t.Skip("Test skipped when running inside a Docker container") - return } testscript.Run(t, testscript.Params{