|
| 1 | +package openapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestRunFormat_ReadableStyle(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + tempDir := t.TempDir() |
| 17 | + inputPath := filepath.Join(tempDir, "input.json") |
| 18 | + outputPath := filepath.Join(tempDir, "output.json") |
| 19 | + input := `{"paths":{},"openapi":"3.0.0","info":{"version":"1","title":"T"}}` |
| 20 | + require.NoError(t, os.WriteFile(inputPath, []byte(input), 0o600)) |
| 21 | + |
| 22 | + err := runFormat(context.Background(), formatFlags{ |
| 23 | + Schema: inputPath, |
| 24 | + Out: outputPath, |
| 25 | + Style: "readable", |
| 26 | + }) |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + actual, err := os.ReadFile(outputPath) |
| 30 | + require.NoError(t, err) |
| 31 | + assert.JSONEq(t, input, string(actual), "readable formatting should preserve the document") |
| 32 | +} |
| 33 | + |
| 34 | +func TestRunFormat_ReadableUppercaseYAMLOutput(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + tempDir := t.TempDir() |
| 38 | + inputPath := filepath.Join(tempDir, "input.json") |
| 39 | + input := `{"paths":{},"openapi":"3.0.0","info":{"version":"1","title":"T"}}` |
| 40 | + require.NoError(t, os.WriteFile(inputPath, []byte(input), 0o600)) |
| 41 | + |
| 42 | + for _, outputName := range []string{"output.YML", "output.YAML"} { |
| 43 | + outputPath := filepath.Join(tempDir, outputName) |
| 44 | + err := runFormat(context.Background(), formatFlags{ |
| 45 | + Schema: inputPath, |
| 46 | + Out: outputPath, |
| 47 | + Style: "readable", |
| 48 | + }) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + actual, err := os.ReadFile(outputPath) |
| 52 | + require.NoError(t, err) |
| 53 | + assert.Contains(t, string(actual), "openapi: 3.0.0", "uppercase YAML extensions should emit YAML") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestRunFormat_SortedYAMLInput(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + tempDir := t.TempDir() |
| 61 | + inputPath := filepath.Join(tempDir, "input.yaml") |
| 62 | + outputPath := filepath.Join(tempDir, "output.json") |
| 63 | + require.NoError(t, os.WriteFile(inputPath, []byte("openapi: 3.0.0\npaths:\n /z: {}\n /a: {}\n"), 0o600)) |
| 64 | + |
| 65 | + err := runFormat(context.Background(), formatFlags{ |
| 66 | + Schema: inputPath, |
| 67 | + Out: outputPath, |
| 68 | + Style: "sorted", |
| 69 | + }) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + actual, err := os.ReadFile(outputPath) |
| 73 | + require.NoError(t, err) |
| 74 | + assert.Equal(t, `{ |
| 75 | + "openapi": "3.0.0", |
| 76 | + "paths": { |
| 77 | + "/a": {}, |
| 78 | + "/z": {} |
| 79 | + } |
| 80 | +} |
| 81 | +`, string(actual)) |
| 82 | +} |
| 83 | + |
| 84 | +func TestRunFormat_SortedRejectsYAMLOutputBeforeCreate(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + tempDir := t.TempDir() |
| 88 | + inputPath := filepath.Join(tempDir, "input.json") |
| 89 | + require.NoError(t, os.WriteFile(inputPath, []byte(`{"openapi":"3.0.0"}`), 0o600)) |
| 90 | + |
| 91 | + for _, outputName := range []string{"output.yaml", "output.YML"} { |
| 92 | + outputPath := filepath.Join(tempDir, outputName) |
| 93 | + err := runFormat(context.Background(), formatFlags{ |
| 94 | + Schema: inputPath, |
| 95 | + Out: outputPath, |
| 96 | + Style: "sorted", |
| 97 | + }) |
| 98 | + require.EqualError(t, err, "sorted formatting only supports JSON output") |
| 99 | + _, statErr := os.Stat(outputPath) |
| 100 | + require.ErrorIs(t, statErr, os.ErrNotExist, "rejected output should not be created") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestRunFormat_RejectsUnknownStyleBeforeCreate(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + |
| 107 | + outputPath := filepath.Join(t.TempDir(), "output.json") |
| 108 | + err := runFormat(context.Background(), formatFlags{ |
| 109 | + Schema: "unused.json", |
| 110 | + Out: outputPath, |
| 111 | + Style: "unknown", |
| 112 | + }) |
| 113 | + require.EqualError(t, err, `unsupported format style "unknown"`) |
| 114 | + _, statErr := os.Stat(outputPath) |
| 115 | + require.ErrorIs(t, statErr, os.ErrNotExist, "rejected output should not be created") |
| 116 | +} |
0 commit comments