Repository: uber-go/zap
File: writer_test.go (Go)

Code before:
		{
			msg:          "file with port",
			paths:        []string{"file://localhost:8080" + tempName},
			wantOtherErr: "ports not allowed",
		},
	}

	for _, tt := range tests {
		t.Run(tt.msg, func(t *testing.T) {
			_, cleanup, err := Open(tt.paths...)
			if err == nil {
				defer cleanup()
			}

			if len(tt.wantNotFoundPaths) == 0 && tt.wantOtherErr == "" {
				assert.NoError(t, err, "Unexpected error opening paths %v.", tt.paths)
				return
			}

			require.Error(t, err)
			if len(tt.wantNotFoundPaths) > 0 {
				errs := multierr.Errors(err)
				require.Len(t, errs, len(tt.wantNotFoundPaths))
				for i, err := range errs {
					assert.ErrorIs(t, err, fs.ErrNotExist)
					assert.Contains(t, err.Error(), tt.wantNotFoundPaths[i], "missing path in error")
				}
			}

			if tt.wantOtherErr != "" {
				msg := err.Error()
				assert.Contains(t, msg, tt.wantOtherErr, "Unexpected error opening paths %v.", tt.paths)
			}
		})
	}

	assert.True(t, fileExists(tempName))
	os.Remove(tempName)
}

func TestOpenRelativePath(t *testing.T) {
	const name = "test-relative-path.txt"

	require.False(t, fileExists(name), "Test file already exists.")
	s, cleanup, err := Open(name)
	require.NoError(t, err, "Open failed.")
	defer func() {
		err := os.Remove(name)
		if !t.Failed() {
			// If the test has already failed, we probably didn't create this file.
			require.NoError(t, err, "Deleting test file failed.")

Review comment (suggestion):
minor: can use assert.ErrorContains here.
```suggestion
					assert.ErrorContains(t, err, tt.wantNotFoundPaths[i], "missing path in error")
```

(If the method is missing, we might need to update testify.)

Code after:
				"/baz/quux",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.msg, func(t *testing.T) {
			_, cleanup, err := Open(tt.paths...)
			if !assert.Error(t, err, "Open must fail.") {
				cleanup()
				return
			}

			errs := multierr.Errors(err)
			require.Len(t, errs, len(tt.wantNotFoundPaths))
			for i, err := range errs {
				assert.ErrorIs(t, err, fs.ErrNotExist)
				assert.ErrorContains(t, err, tt.wantNotFoundPaths[i], "missing path in error")
			}
		})
	}
}

func TestOpenRelativePath(t *testing.T) {
	const name = "test-relative-path.txt"

	require.False(t, fileExists(name), "Test file already exists.")
	s, cleanup, err := Open(name)
	require.NoError(t, err, "Open failed.")
	defer func() {
		err := os.Remove(name)
		if !t.Failed() {
			// If the test has already failed, we probably didn't create this file.
			require.NoError(t, err, "Deleting test file failed.")
		}
	}()
	defer cleanup()

	_, err = s.Write([]byte("test"))
	assert.NoError(t, err, "Write failed.")
	assert.True(t, fileExists(name), "Didn't create file for relative path.")
}

func TestOpenFails(t *testing.T) {
	tests := []struct {
		paths []string
	}{
		{paths: []string{"./non-existent-dir/file"}},           // directory doesn't exist
		{paths: []string{"stdout", "./non-existent-dir/file"}}, // directory doesn't exist
		{paths: []string{"://foo.log"}},                        // invalid URL, scheme can't begin with colon
		{paths: []string{"mem://somewhere"}},                   // scheme not registered