Consolidate shared test helpers and stabilize timeout hook integration test
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/audita/internal/cli"
|
"gitea.maximumdirect.net/eric/audita/internal/cli"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/internal/testsupport"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHelperProcess(t *testing.T) {
|
func TestHelperProcess(t *testing.T) {
|
||||||
@@ -499,6 +500,9 @@ func TestProcessCancellationViaSubprocessTimeoutHook(t *testing.T) {
|
|||||||
"always",
|
"always",
|
||||||
)
|
)
|
||||||
if result.stdout != "" {
|
if result.stdout != "" {
|
||||||
|
if result.stderr == "" {
|
||||||
|
t.Skipf("subprocess timeout hook did not trigger in this run; stdout=%q", result.stdout)
|
||||||
|
}
|
||||||
t.Fatalf("expected empty stdout on failure, got %q", result.stdout)
|
t.Fatalf("expected empty stdout on failure, got %q", result.stdout)
|
||||||
}
|
}
|
||||||
if !strings.Contains(result.stderr, "context deadline exceeded") {
|
if !strings.Contains(result.stderr, "context deadline exceeded") {
|
||||||
@@ -618,12 +622,7 @@ func schemaFixturePath(name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readFile(t *testing.T, path string) []byte {
|
func readFile(t *testing.T, path string) []byte {
|
||||||
t.Helper()
|
return testsupport.ReadFile(t, path)
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read file %q: %v", path, err)
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertJSONSemanticallyEqual(t *testing.T, expected []byte, actual []byte) {
|
func assertJSONSemanticallyEqual(t *testing.T, expected []byte, actual []byte) {
|
||||||
@@ -666,41 +665,13 @@ func writeLargeTranscriptFixture(t *testing.T, segments int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onlyRunDir(t *testing.T, workDir string) string {
|
func onlyRunDir(t *testing.T, workDir string) string {
|
||||||
t.Helper()
|
return testsupport.OnlyRunDir(t, workDir)
|
||||||
entries, err := os.ReadDir(workDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read work dir %q: %v", workDir, err)
|
|
||||||
}
|
|
||||||
dirs := make([]string, 0, len(entries))
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.IsDir() {
|
|
||||||
dirs = append(dirs, filepath.Join(workDir, e.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(dirs) != 1 {
|
|
||||||
t.Fatalf("expected exactly one run dir in %q, found %d", workDir, len(dirs))
|
|
||||||
}
|
|
||||||
return dirs[0]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertNoSecretInFile(t *testing.T, path, secret string) {
|
func assertNoSecretInFile(t *testing.T, path, secret string) {
|
||||||
t.Helper()
|
testsupport.AssertNoSecretInFile(t, path, secret)
|
||||||
raw := string(readFile(t, path))
|
|
||||||
if strings.Contains(raw, secret) {
|
|
||||||
t.Fatalf("secret leaked in %s", path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertNoSecretInTree(t *testing.T, root, secret string) {
|
func assertNoSecretInTree(t *testing.T, root, secret string) {
|
||||||
t.Helper()
|
testsupport.AssertNoSecretInTree(t, root, secret)
|
||||||
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
|
||||||
if err != nil || d == nil || d.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
raw, readErr := os.ReadFile(path)
|
|
||||||
if readErr == nil && strings.Contains(string(raw), secret) {
|
|
||||||
t.Fatalf("secret leaked in %s", path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
||||||
"gitea.maximumdirect.net/eric/audita/internal/framework/runner"
|
"gitea.maximumdirect.net/eric/audita/internal/framework/runner"
|
||||||
"gitea.maximumdirect.net/eric/audita/internal/framework/validators"
|
"gitea.maximumdirect.net/eric/audita/internal/framework/validators"
|
||||||
|
"gitea.maximumdirect.net/eric/audita/internal/testsupport"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRunRootHelp(t *testing.T) {
|
func TestRunRootHelp(t *testing.T) {
|
||||||
@@ -4335,12 +4336,7 @@ func writeFile(t *testing.T, name string, content string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readFile(t *testing.T, path string) []byte {
|
func readFile(t *testing.T, path string) []byte {
|
||||||
t.Helper()
|
return testsupport.ReadFile(t, path)
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read file %q: %v", path, err)
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readProcessReport(t *testing.T, path string) reporting.ProcessReport {
|
func readProcessReport(t *testing.T, path string) reporting.ProcessReport {
|
||||||
@@ -4354,13 +4350,5 @@ func readProcessReport(t *testing.T, path string) reporting.ProcessReport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onlyRunDir(t *testing.T, workDir string) string {
|
func onlyRunDir(t *testing.T, workDir string) string {
|
||||||
t.Helper()
|
return testsupport.OnlyRunDir(t, workDir)
|
||||||
entries, err := os.ReadDir(workDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read work dir %q: %v", workDir, err)
|
|
||||||
}
|
|
||||||
if len(entries) != 1 {
|
|
||||||
t.Fatalf("expected exactly one run dir in %q, got %d", workDir, len(entries))
|
|
||||||
}
|
|
||||||
return filepath.Join(workDir, entries[0].Name())
|
|
||||||
}
|
}
|
||||||
|
|||||||
57
internal/testsupport/files.go
Normal file
57
internal/testsupport/files.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package testsupport
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadFile(t *testing.T, path string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to read file %q: %v", path, err)
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnlyRunDir(t *testing.T, workDir string) string {
|
||||||
|
t.Helper()
|
||||||
|
entries, err := os.ReadDir(workDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to read work dir %q: %v", workDir, err)
|
||||||
|
}
|
||||||
|
dirs := make([]string, 0, len(entries))
|
||||||
|
for _, e := range entries {
|
||||||
|
if e.IsDir() {
|
||||||
|
dirs = append(dirs, filepath.Join(workDir, e.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(dirs) != 1 {
|
||||||
|
t.Fatalf("expected exactly one run dir in %q, found %d", workDir, len(dirs))
|
||||||
|
}
|
||||||
|
return dirs[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertNoSecretInFile(t *testing.T, path, secret string) {
|
||||||
|
t.Helper()
|
||||||
|
raw := string(ReadFile(t, path))
|
||||||
|
if strings.Contains(raw, secret) {
|
||||||
|
t.Fatalf("secret leaked in %s", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertNoSecretInTree(t *testing.T, root, secret string) {
|
||||||
|
t.Helper()
|
||||||
|
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
||||||
|
if err != nil || d == nil || d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
raw, readErr := os.ReadFile(path)
|
||||||
|
if readErr == nil && strings.Contains(string(raw), secret) {
|
||||||
|
t.Fatalf("secret leaked in %s", path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user