MCPcopy
hub / github.com/keploy/keploy / TestReadFileAnyPrefersPreferred

Function TestReadFileAnyPrefersPreferred

pkg/platform/yaml/codec_test.go:502–542  ·  view source on GitHub ↗

TestReadFileAnyPrefersPreferred verifies ReadFileAny picks the preferred format when both files exist.

(t *testing.T)

Source from the content-addressed store, hash-verified

500// TestReadFileAnyPrefersPreferred verifies ReadFileAny picks the preferred
501// format when both files exist.
502func TestReadFileAnyPrefersPreferred(t *testing.T) {
503 ctx := context.Background()
504 logger := testLogger()
505
506 tempDir, err := os.MkdirTemp("", "readfile_any_prefer_test")
507 if err != nil {
508 t.Fatal(err)
509 }
510 defer os.RemoveAll(tempDir)
511
512 if err := os.WriteFile(filepath.Join(tempDir, "f.yaml"), []byte("y: 1"), 0644); err != nil {
513 t.Fatal(err)
514 }
515 if err := os.WriteFile(filepath.Join(tempDir, "f.json"), []byte(`{"j":1}`), 0644); err != nil {
516 t.Fatal(err)
517 }
518
519 // Prefer JSON -> should return .json contents
520 data, detected, err := ReadFileAny(ctx, logger, tempDir, "f", FormatJSON)
521 if err != nil {
522 t.Fatal(err)
523 }
524 if detected != FormatJSON {
525 t.Errorf("detected = %v, want FormatJSON", detected)
526 }
527 if string(data) != `{"j":1}` {
528 t.Errorf("data = %q", data)
529 }
530
531 // Prefer YAML -> should return .yaml contents
532 data, detected, err = ReadFileAny(ctx, logger, tempDir, "f", FormatYAML)
533 if err != nil {
534 t.Fatal(err)
535 }
536 if detected != FormatYAML {
537 t.Errorf("detected = %v, want FormatYAML", detected)
538 }
539 if string(data) != "y: 1" {
540 t.Errorf("data = %q", data)
541 }
542}
543
544// TestReadFileAnyFallsBack verifies ReadFileAny falls back to the other
545// format when the preferred one is missing — this is the backward-compat

Callers

nothing calls this directly

Calls 2

ReadFileAnyFunction · 0.85
testLoggerFunction · 0.70

Tested by

no test coverage detected