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

Code before:

	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.")
		}
	}()
	defer cleanup()

	_, err = s.Write([]byte("test"))
	assert.NoError(t, err, "Write failed.")

Review comment (suggestion):
same, can use assert.ErrorContains.
```suggestion
				assert.ErrorContains(t, err, tt.wantOtherErr, "Unexpected error opening paths %v.", tt.paths)
```

Code after:
		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
	}

	for _, tt := range tests {
		_, cleanup, err := Open(tt.paths...)
		require.Nil(t, cleanup, "Cleanup function should never be nil")
		assert.Error(t, err, "Open with invalid URL should fail.")