]> Cypherpunks repositories - gostls13.git/commitdiff
os: use relative paths in a test dir in TestOpenError
authorDamien Neil <dneil@google.com>
Mon, 14 Oct 2024 22:30:13 +0000 (15:30 -0700)
committerDamien Neil <dneil@google.com>
Tue, 15 Oct 2024 17:23:27 +0000 (17:23 +0000)
Refactor TestOpenError to use relative paths in test cases,
in preparation for extending it to test os.Root.

Use a test temporary directory instead of system directory
with presumed-known contents.

Move the testcase type and case definitions inline with the test.

For #67002

Change-Id: Idc53dd9fcecf763d3e4eb3b4643032e3003d7ef4
Reviewed-on: https://go-review.googlesource.com/c/go/+/620157
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/os/os_test.go

index e7d8e55094488d7fa3bd95e0b1040979ab37ae67..4db5521fee71af0efb69ae7994028f96df7d448c 100644 (file)
@@ -1777,35 +1777,36 @@ func TestSeekError(t *testing.T) {
        }
 }
 
-type openErrorTest struct {
-       path  string
-       mode  int
-       error error
-}
+func TestOpenError(t *testing.T) {
+       t.Parallel()
+
+       dir := t.TempDir()
+       if err := WriteFile(filepath.Join(dir, "is-a-file"), nil, 0o666); err != nil {
+               t.Fatal(err)
+       }
+       if err := Mkdir(filepath.Join(dir, "is-a-dir"), 0o777); err != nil {
+               t.Fatal(err)
+       }
 
-var openErrorTests = []openErrorTest{
-       {
-               sfdir + "/no-such-file",
+       for _, tt := range []struct {
+               path  string
+               mode  int
+               error error
+       }{{
+               "no-such-file",
                O_RDONLY,
                syscall.ENOENT,
-       },
-       {
-               sfdir,
+       }, {
+               "is-a-dir",
                O_WRONLY,
                syscall.EISDIR,
-       },
-       {
-               sfdir + "/" + sfname + "/no-such-file",
+       }, {
+               "is-a-file/no-such-file",
                O_WRONLY,
                syscall.ENOTDIR,
-       },
-}
-
-func TestOpenError(t *testing.T) {
-       t.Parallel()
-
-       for _, tt := range openErrorTests {
-               f, err := OpenFile(tt.path, tt.mode, 0)
+       }} {
+               path := filepath.Join(dir, tt.path)
+               f, err := OpenFile(path, tt.mode, 0)
                if err == nil {
                        t.Errorf("Open(%q, %d) succeeded", tt.path, tt.mode)
                        f.Close()