Separate output checks from session creation
This commit is contained in:
@@ -33,20 +33,20 @@ func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testi
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, output := range []string{input, alias, hardlink} {
|
||||
if _, err := PrepareOutput(OutputSelection{TranscriptPath: output, InputTranscriptPath: input}); err != nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, InputTranscriptPath: input}); err != nil {
|
||||
t.Fatalf("in-place output rejected: %s: %v", output, err)
|
||||
}
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, ErrorsFile), ReportDir: dir, ReportingEnabled: true}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, ErrorsFile), ReportDir: dir, ReportingEnabled: true}); err == nil {
|
||||
t.Fatal("report/transcript collision accepted")
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, ProtectedPaths: []string{filepath.Join(dir, ErrorsFile)}}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true, ProtectedPaths: []string{filepath.Join(dir, ErrorsFile)}}); err == nil {
|
||||
t.Fatal("report/source collision accepted")
|
||||
}
|
||||
if err := os.Symlink(input, filepath.Join(dir, ErrorsFile)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, InputTranscriptPath: input}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true, InputTranscriptPath: input}); err == nil {
|
||||
t.Fatal("report/input alias accepted")
|
||||
}
|
||||
if err := os.Remove(filepath.Join(dir, ErrorsFile)); err != nil {
|
||||
@@ -55,7 +55,7 @@ func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testi
|
||||
if err := os.Mkdir(filepath.Join(dir, MarkdownFile), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}); err == nil {
|
||||
t.Fatal("directory report target accepted")
|
||||
}
|
||||
if got, err := os.ReadFile(input); err != nil || string(got) != "source" {
|
||||
@@ -69,7 +69,7 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) {
|
||||
if err := os.WriteFile(output, []byte("old content"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: output})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -85,10 +85,10 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) {
|
||||
if info, _ := os.Stat(output); info.Mode().Perm() != 0600 {
|
||||
t.Fatalf("permission changed: %v", info.Mode())
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "missing", "out.json")}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, "missing", "out.json")}); err == nil {
|
||||
t.Fatal("absent parent accepted")
|
||||
}
|
||||
if _, err := PrepareOutput(OutputSelection{TranscriptPath: dir}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: dir}); err == nil {
|
||||
t.Fatal("directory target accepted")
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
@@ -115,7 +115,7 @@ func TestOutputPathsUseFilesystemParent(t *testing.T) {
|
||||
}
|
||||
parent := alias + string(filepath.Separator) + ".."
|
||||
output := parent + string(filepath.Separator) + "out.json"
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -146,7 +146,7 @@ func TestOutputPublishesThroughTranscriptAndReportSymlinks(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -196,7 +196,7 @@ func TestStagingCleansTemporaryResourcesAfterPublicationFailure(t *testing.T) {
|
||||
|
||||
func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "out.json"), ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: false})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, "out.json"), ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: false})
|
||||
if err != nil || len(session.ReportPaths()) != 0 {
|
||||
t.Fatalf("disabled reports probed: %+v %v", session, err)
|
||||
}
|
||||
@@ -210,7 +210,7 @@ func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) {
|
||||
|
||||
func TestEnabledReportingRequiresExistingDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, err := PrepareOutput(OutputSelection{ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: true}); err == nil {
|
||||
if _, err := prepareOutputForTest(OutputSelection{ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: true}); err == nil {
|
||||
t.Fatal("missing report directory accepted")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) {
|
||||
@@ -220,7 +220,7 @@ func TestEnabledReportingRequiresExistingDirectory(t *testing.T) {
|
||||
|
||||
func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -251,7 +251,7 @@ func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) {
|
||||
|
||||
func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -286,7 +286,7 @@ func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) {
|
||||
func TestFinalizationCollectsFailuresAndRetainsPublishedTranscript(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
output := filepath.Join(dir, "output.json")
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -330,7 +330,7 @@ func TestFailedRenderingKeepsInitializedEvidenceAfterStreamError(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
delivery := errors.New("stream delivery failed")
|
||||
rendering := errors.New("failed report rendering failed")
|
||||
session, err := PrepareOutput(OutputSelection{
|
||||
session, err := prepareOutputForTest(OutputSelection{
|
||||
Stdout: writerFunc(func([]byte) (int, error) { return 0, delivery }),
|
||||
ReportDir: dir, ReportingEnabled: true,
|
||||
})
|
||||
@@ -390,7 +390,7 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) {
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -430,7 +430,7 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) {
|
||||
|
||||
func TestRepeatedFailureOfOneReportCountsOnce(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -471,7 +471,7 @@ func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) {
|
||||
defer cancel()
|
||||
delivery := errors.New("late stream error")
|
||||
var escaped []byte
|
||||
session, err := PrepareOutput(OutputSelection{
|
||||
session, err := prepareOutputForTest(OutputSelection{
|
||||
Stdout: writerFunc(func(payload []byte) (int, error) {
|
||||
escaped = append(escaped, payload...)
|
||||
cancel()
|
||||
@@ -502,7 +502,7 @@ func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCleanupFailureKeepsTemporaryDirectoryForRetry(t *testing.T) {
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -600,13 +600,14 @@ func TestSelectionAndAbortPreserveTranscript(t *testing.T) {
|
||||
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := SelectOutput(OutputSelection{TranscriptPath: output})
|
||||
checked, err := CheckOutput(OutputSelection{TranscriptPath: output})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entries, err := os.ReadDir(dir); err != nil || len(entries) != 1 {
|
||||
t.Fatalf("selection changed directory: %v %v", entries, err)
|
||||
}
|
||||
session := NewOutputSession(checked)
|
||||
if err := session.Prepare(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -627,7 +628,7 @@ func TestSelectionAndAbortPreserveTranscript(t *testing.T) {
|
||||
|
||||
func TestStdoutAndDeviceDelivery(t *testing.T) {
|
||||
var stdout bytes.Buffer
|
||||
session, err := PrepareOutput(OutputSelection{Stdout: &stdout})
|
||||
session, err := prepareOutputForTest(OutputSelection{Stdout: &stdout})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -637,7 +638,7 @@ func TestStdoutAndDeviceDelivery(t *testing.T) {
|
||||
if stdout.String() != "transcript" {
|
||||
t.Fatalf("stdout=%q", stdout.String())
|
||||
}
|
||||
session, err = PrepareOutput(OutputSelection{TranscriptPath: os.DevNull})
|
||||
session, err = prepareOutputForTest(OutputSelection{TranscriptPath: os.DevNull})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -655,7 +656,7 @@ func TestFinishRetainsPrimaryFailureAndAttemptsReports(t *testing.T) {
|
||||
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -707,7 +708,7 @@ type shortOutput struct{}
|
||||
func (shortOutput) Write(b []byte) (int, error) { return len(b) / 2, nil }
|
||||
|
||||
func TestShortStdoutWriteIsFailure(t *testing.T) {
|
||||
session, err := PrepareOutput(OutputSelection{Stdout: shortOutput{}})
|
||||
session, err := prepareOutputForTest(OutputSelection{Stdout: shortOutput{}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -723,7 +724,7 @@ func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) {
|
||||
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: output})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -746,7 +747,7 @@ func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) {
|
||||
func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) {
|
||||
pipe := filepath.Join(t.TempDir(), "output-pipe")
|
||||
makeNamedPipe(t, pipe)
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: pipe})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: pipe})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -761,7 +762,7 @@ func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) {
|
||||
func TestLateNamedPipeIsPreserved(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "output")
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: path})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: path})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -784,7 +785,7 @@ func TestLateNamedPipeIsPreserved(t *testing.T) {
|
||||
func TestStreamChangedToRegularFileIsPreserved(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "output")
|
||||
makeNamedPipe(t, path)
|
||||
session, err := PrepareOutput(OutputSelection{TranscriptPath: path})
|
||||
session, err := prepareOutputForTest(OutputSelection{TranscriptPath: path})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -814,3 +815,15 @@ func makeNamedPipe(t *testing.T, path string) {
|
||||
t.Fatalf("mkfifo: %v %s", err, output)
|
||||
}
|
||||
}
|
||||
|
||||
func prepareOutputForTest(selection OutputSelection) (*OutputSession, error) {
|
||||
checked, err := CheckOutput(selection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session := NewOutputSession(checked)
|
||||
if err := session.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user