-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-service: Make target destination when writing JSON/binary explicit (…
…#11800) Avoids being surprised by the special handling for - and empty string output paths.
- Loading branch information
Showing
11 changed files
with
192 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ioutil | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
var ( | ||
stdOutStream OutputTarget = func() (io.Writer, io.Closer, Aborter, error) { | ||
return os.Stdout, &noopCloser{}, func() {}, nil | ||
} | ||
) | ||
|
||
type Aborter func() | ||
|
||
type OutputTarget func() (io.Writer, io.Closer, Aborter, error) | ||
|
||
func NoOutputStream() OutputTarget { | ||
return func() (io.Writer, io.Closer, Aborter, error) { | ||
return nil, nil, nil, nil | ||
} | ||
} | ||
|
||
func ToAtomicFile(path string, perm os.FileMode) OutputTarget { | ||
return func() (io.Writer, io.Closer, Aborter, error) { | ||
f, err := NewAtomicWriterCompressed(path, perm) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
return f, f, func() { _ = f.Abort() }, nil | ||
} | ||
} | ||
|
||
func ToStdOut() OutputTarget { | ||
return stdOutStream | ||
} | ||
|
||
func ToStdOutOrFileOrNoop(outputPath string, perm os.FileMode) OutputTarget { | ||
if outputPath == "" { | ||
return NoOutputStream() | ||
} else if outputPath == "-" { | ||
return ToStdOut() | ||
} else { | ||
return ToAtomicFile(outputPath, perm) | ||
} | ||
} | ||
|
||
type noopCloser struct{} | ||
|
||
func (c *noopCloser) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package ioutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNoOutputStream(t *testing.T) { | ||
writer, closer, aborter, err := NoOutputStream()() | ||
require.NoError(t, err) | ||
require.Nil(t, writer) | ||
require.Nil(t, closer) | ||
require.Nil(t, aborter) | ||
} | ||
|
||
func TestToStdOut(t *testing.T) { | ||
writer, closer, aborter, err := ToStdOut()() | ||
require.NoError(t, err) | ||
require.Same(t, os.Stdout, writer) | ||
|
||
// Should not close StdOut | ||
require.NoError(t, closer.Close()) | ||
_, err = os.Stdout.WriteString("TestToStdOut After Close\n") | ||
require.NoError(t, err) | ||
|
||
aborter() | ||
_, err = os.Stdout.WriteString("TestToStdOut After Abort\n") | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestToAtomicFile(t *testing.T) { | ||
t.Run("Abort", func(t *testing.T) { | ||
dir := t.TempDir() | ||
path := filepath.Join(dir, "test.txt") | ||
writer, closer, aborter, err := ToAtomicFile(path, 0o644)() | ||
defer closer.Close() | ||
require.NoError(t, err) | ||
|
||
expected := []byte("test") | ||
_, err = writer.Write(expected) | ||
require.NoError(t, err) | ||
aborter() | ||
|
||
_, err = os.Stat(path) | ||
require.ErrorIs(t, err, os.ErrNotExist, "Should not have written file") | ||
}) | ||
|
||
t.Run("Close", func(t *testing.T) { | ||
dir := t.TempDir() | ||
path := filepath.Join(dir, "test.txt") | ||
writer, closer, _, err := ToAtomicFile(path, 0o644)() | ||
defer closer.Close() | ||
require.NoError(t, err) | ||
|
||
expected := []byte("test") | ||
_, err = writer.Write(expected) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(path) | ||
require.ErrorIs(t, err, os.ErrNotExist, "Target file should not exist prior to Close") | ||
|
||
require.NoError(t, closer.Close()) | ||
actual, err := os.ReadFile(path) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestToStdOutOrFileOrNoop(t *testing.T) { | ||
t.Run("EmptyOutputPath", func(t *testing.T) { | ||
writer, _, _, err := ToStdOutOrFileOrNoop("", 0o644)() | ||
require.NoError(t, err) | ||
require.Nil(t, writer, "Should use no output stream") | ||
}) | ||
|
||
t.Run("StdOut", func(t *testing.T) { | ||
writer, _, _, err := ToStdOutOrFileOrNoop("-", 0o644)() | ||
require.NoError(t, err) | ||
require.Same(t, os.Stdout, writer, "Should use std out") | ||
}) | ||
|
||
t.Run("File", func(t *testing.T) { | ||
dir := t.TempDir() | ||
path := filepath.Join(dir, "test.txt") | ||
writer, closer, _, err := ToStdOutOrFileOrNoop(path, 0o644)() | ||
defer closer.Close() | ||
require.NoError(t, err) | ||
|
||
expected := []byte("test") | ||
_, err = writer.Write(expected) | ||
require.NoError(t, err) | ||
require.NoError(t, closer.Close()) | ||
actual, err := os.ReadFile(path) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, actual) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.