49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package preflight
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
|
)
|
|
|
|
func TestReadinessAndCollisionPrecedeOutputPreparation(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "output.json")
|
|
if _, err := Prepare(context.Background(), Input{Config: &config.Config{}, Output: diagnostics.OutputSelection{TranscriptPath: output}}); err == nil {
|
|
t.Fatal("missing transcript accepted")
|
|
}
|
|
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
|
t.Fatalf("output created: %v", err)
|
|
}
|
|
if _, err := Prepare(context.Background(), Input{Config: &config.Config{}, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output, ProtectedPaths: []string{output}}}); err == nil {
|
|
t.Fatal("collision accepted")
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil || len(entries) != 0 {
|
|
t.Fatalf("prepared before check: %v %v", entries, err)
|
|
}
|
|
}
|
|
|
|
func TestOptionalDebugFailureDoesNotPreventPreparation(t *testing.T) {
|
|
dir := t.TempDir()
|
|
blocked := filepath.Join(dir, "file")
|
|
if err := os.WriteFile(blocked, []byte("x"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := Prepare(context.Background(), Input{Config: &config.Config{}, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: filepath.Join(dir, "output")}, Debug: &DebugInput{WorkDir: blocked, RunID: "run"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(result.Warnings) == 0 || result.Debug != nil {
|
|
t.Fatalf("debug=%v warnings=%v", result.Debug, result.Warnings)
|
|
}
|
|
if err := result.Output.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|