Skip to content
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
21 changes: 21 additions & 0 deletions internal/testutil/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package testutil provides small helpers shared by aict's tool test suites.
package testutil

import (
"path/filepath"
"testing"
)

// MissingPath returns an absolute path, ending in name, that is guaranteed not
// to exist for the duration of the test.
//
// Tests must not hardcode paths such as "/nonexistent/file.txt". On Windows
// that resolves to an ordinary, creatable location (C:\nonexistent\file.txt)
// which may genuinely exist on a developer's machine — silently inverting the
// assertion, so a "missing path" test starts exercising a real file instead.
// Anchoring to t.TempDir() gives a directory the test framework created empty
// and removes on cleanup, so name is reliably absent on every platform.
func MissingPath(t *testing.T, name string) string {
t.Helper()
return filepath.Join(t.TempDir(), name)
}
4 changes: 3 additions & 1 deletion tools/awk/awk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -131,7 +133,7 @@ func TestAwk_NR(t *testing.T) {
}

func TestAwk_MissingFile(t *testing.T) {
result := runAwk(t, []string{"{print $1}", "/nonexistent/file.txt"})
result := runAwk(t, []string{"{print $1}", testutil.MissingPath(t, "file.txt")})
if len(result.Errors) == 0 {
t.Error("expected error for non-existent file")
}
Expand Down
4 changes: 3 additions & 1 deletion tools/cat/cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -109,7 +111,7 @@ func TestCat_Directory(t *testing.T) {
}

func TestCat_Missing(t *testing.T) {
result, err := catFile("/nonexistent/missing.txt", Config{})
result, err := catFile(testutil.MissingPath(t, "missing.txt"), Config{})
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion tools/checksums/checksums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

pathutil "github.com/synseqack/aict/internal/path"
"github.com/synseqack/aict/internal/testutil"
)

func runChecksums(args []string) (string, error) {
Expand Down Expand Up @@ -80,7 +81,7 @@ func TestChecksums_NonExistent(t *testing.T) {
os.Setenv("AICT_XML", "1")
defer os.Unsetenv("AICT_XML")

output, err := runChecksums([]string{"/nonexistent/file.txt"})
output, err := runChecksums([]string{testutil.MissingPath(t, "file.txt")})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -127,7 +129,7 @@ func TestDiff_MissingFile(t *testing.T) {
dir := t.TempDir()
existing := createFile(t, dir, "a.txt", "hello\n")

result := runDiff(t, []string{existing, "/nonexistent/missing.txt"})
result := runDiff(t, []string{existing, testutil.MissingPath(t, "missing.txt")})
if len(result.Errors) == 0 {
t.Error("expected error for missing file")
}
Expand Down
4 changes: 3 additions & 1 deletion tools/du/du_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func runDu(args []string) (string, error) {
Expand Down Expand Up @@ -156,7 +158,7 @@ func TestDu_NonExistent(t *testing.T) {
r, w, _ := os.Pipe()
os.Stdout = w

err := Run([]string{"/nonexistent/path"})
err := Run([]string{testutil.MissingPath(t, "path")})

w.Close()
os.Stdout = oldStdout
Expand Down
4 changes: 3 additions & 1 deletion tools/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"runtime"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func runFile(args []string) (string, error) {
Expand Down Expand Up @@ -156,7 +158,7 @@ func TestFile_Executable(t *testing.T) {
}

func TestFile_NonExistent(t *testing.T) {
result, err := runFileWithOutput("/nonexistent/path/file.txt", Config{XML: true})
result, err := runFileWithOutput(testutil.MissingPath(t, "file.txt"), Config{XML: true})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/find/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -147,7 +149,7 @@ func TestFind_Invert(t *testing.T) {
}

func TestFind_Missing(t *testing.T) {
result := runFind(t, []string{"/nonexistent/directory"})
result := runFind(t, []string{testutil.MissingPath(t, "directory")})
if len(result.Errors) == 0 {
t.Error("expected error for non-existent root")
}
Expand Down
4 changes: 3 additions & 1 deletion tools/head/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func runHead(args []string) (string, error) {
Expand Down Expand Up @@ -124,7 +126,7 @@ func TestHead_EmptyFile(t *testing.T) {
}

func TestHead_NonExistent(t *testing.T) {
result, err := runHeadWithOutput("/nonexistent/file.txt", Config{XML: true})
result, err := runHeadWithOutput(testutil.MissingPath(t, "file.txt"), Config{XML: true})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/ls/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"
"time"

"github.com/synseqack/aict/internal/testutil"
)

func runLS(args []string) (string, error) {
Expand Down Expand Up @@ -171,7 +173,7 @@ func TestLS_Symlinks(t *testing.T) {
}

func TestLS_Error_NonExistent(t *testing.T) {
result, err := runLSWithOutput([]string{"/nonexistent/path/that/does/not/exist"}, Config{XML: true})
result, err := runLSWithOutput([]string{testutil.MissingPath(t, "does-not-exist")}, Config{XML: true})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/realpath/realpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func runRealpath(args []string) (string, error) {
Expand Down Expand Up @@ -64,7 +66,7 @@ func TestRealpath_Directory(t *testing.T) {
}

func TestRealpath_NonExistent(t *testing.T) {
result, err := runRealpathWithOutput("/nonexistent/path/file.txt")
result, err := runRealpathWithOutput(testutil.MissingPath(t, "file.txt"))
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/sed/sed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -167,7 +169,7 @@ func TestSed_RegexAddress(t *testing.T) {
}

func TestSed_MissingFile(t *testing.T) {
result := runSed(t, []string{"-e", "s/a/b/", "/nonexistent/file.txt"})
result := runSed(t, []string{"-e", "s/a/b/", testutil.MissingPath(t, "file.txt")})
if len(result.Errors) == 0 {
t.Error("expected error for non-existent file")
}
Expand Down
4 changes: 3 additions & 1 deletion tools/stat/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -98,7 +100,7 @@ func TestStat_FollowSymlink(t *testing.T) {
}

func TestStat_Missing(t *testing.T) {
result, err := statPath("/nonexistent/missing.txt", Config{})
result, err := statPath(testutil.MissingPath(t, "missing.txt"), Config{})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func runTail(args []string) (string, error) {
Expand Down Expand Up @@ -118,7 +120,7 @@ func TestTail_EmptyFile(t *testing.T) {
}

func TestTail_NonExistent(t *testing.T) {
result, err := runTailWithOutput("/nonexistent/file.txt", Config{XML: true})
result, err := runTailWithOutput(testutil.MissingPath(t, "file.txt"), Config{XML: true})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/tar/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createTar(t *testing.T, dir, name string, files map[string]string) string {
Expand Down Expand Up @@ -122,7 +124,7 @@ func TestTar_FileTypes(t *testing.T) {
}

func TestTar_NonExistent(t *testing.T) {
result := runTar(t, []string{"/nonexistent/archive.tar"})
result := runTar(t, []string{testutil.MissingPath(t, "archive.tar")})
if len(result.Errors) == 0 {
t.Error("expected error for non-existent archive")
}
Expand Down
4 changes: 3 additions & 1 deletion tools/wc/wc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/synseqack/aict/internal/testutil"
)

func createFile(t *testing.T, dir, name, content string) string {
Expand Down Expand Up @@ -141,7 +143,7 @@ func TestWC_Directory(t *testing.T) {
}

func TestWC_Missing(t *testing.T) {
result, err := runWC([]string{"/nonexistent/file.txt"}, Config{Lines: true, Words: true, Bytes: true})
result, err := runWC([]string{testutil.MissingPath(t, "file.txt")}, Config{Lines: true, Words: true, Bytes: true})
if err != nil {
t.Fatal(err)
}
Expand Down
Loading