(t *testing.T)
| 25 | ) |
| 26 | |
| 27 | func TestInitialize(t *testing.T) { |
| 28 | tempDir := t.TempDir() |
| 29 | |
| 30 | t.Run("embedded rego", func(t *testing.T) { |
| 31 | opts := &InitOptions{ |
| 32 | Directory: tempDir, |
| 33 | Embedded: true, |
| 34 | Name: "test-policy", |
| 35 | Description: "test description", |
| 36 | } |
| 37 | |
| 38 | err := Initialize(opts) |
| 39 | require.NoError(t, err) |
| 40 | |
| 41 | policyPath := filepath.Join(tempDir, "test-policy.yaml") |
| 42 | assert.FileExists(t, policyPath) |
| 43 | }) |
| 44 | |
| 45 | t.Run("standalone rego file", func(t *testing.T) { |
| 46 | opts := &InitOptions{ |
| 47 | Directory: tempDir, |
| 48 | Embedded: false, |
| 49 | Name: "standalone-rego", |
| 50 | } |
| 51 | |
| 52 | err := Initialize(opts) |
| 53 | require.NoError(t, err) |
| 54 | |
| 55 | assert.FileExists(t, filepath.Join(tempDir, "standalone-rego.yaml")) |
| 56 | assert.FileExists(t, filepath.Join(tempDir, "standalone-rego.rego")) |
| 57 | }) |
| 58 | |
| 59 | t.Run("file exists and no force", func(t *testing.T) { |
| 60 | opts := &InitOptions{ |
| 61 | Directory: tempDir, |
| 62 | Name: "duplicate", |
| 63 | } |
| 64 | |
| 65 | // First time should succeed |
| 66 | err := Initialize(opts) |
| 67 | require.NoError(t, err) |
| 68 | |
| 69 | // Second time should fail |
| 70 | err = Initialize(opts) |
| 71 | require.Error(t, err) |
| 72 | assert.Contains(t, err.Error(), "already exists") |
| 73 | |
| 74 | // With force it should succeed |
| 75 | opts.Force = true |
| 76 | err = Initialize(opts) |
| 77 | require.NoError(t, err) |
| 78 | }) |
| 79 | |
| 80 | t.Run("name and description are properly set", func(t *testing.T) { |
| 81 | customName := "custom-policy-name" |
| 82 | customDesc := "This is a custom policy description" |
| 83 | |
| 84 | opts := &InitOptions{ |
nothing calls this directly
no test coverage detected