|
| 1 | +package manifest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/thomaspoignant/go-feature-flag/cmd/cli/generate/manifest" |
| 10 | +) |
| 11 | + |
| 12 | +func TestManifestCmd(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + args []string |
| 16 | + expectedManifest string |
| 17 | + expectedOutput string |
| 18 | + assertError assert.ErrorAssertionFunc |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "should return success if everything is ok", |
| 22 | + args: []string{"--config=testdata/input/flag.goff.yaml"}, |
| 23 | + expectedManifest: "testdata/output/flag.goff.json", |
| 24 | + expectedOutput: "🎉 Manifest has been created\n", |
| 25 | + assertError: assert.NoError, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "should ignore flag without value", |
| 29 | + args: []string{"--config=testdata/input/flag-no-default.yaml"}, |
| 30 | + expectedManifest: "testdata/output/flag-no-default.json", |
| 31 | + expectedOutput: "⚠️ flag test-flag ignored: no default value provided\n🎉 Manifest has been created\n", |
| 32 | + assertError: assert.NoError, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "should error if flag type is invalid", |
| 36 | + args: []string{"--config=testdata/input/flag-invalid-flag-type.yaml"}, |
| 37 | + assertError: assert.Error, |
| 38 | + expectedOutput: "Error: invalid configuration for flag test-flag: impossible to find type\n", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "should have int as type if float with .0 and int are mixed", |
| 42 | + args: []string{"--config=testdata/input/flag-int-as-float.yaml"}, |
| 43 | + assertError: assert.NoError, |
| 44 | + expectedManifest: "testdata/output/flag-int-as-float.json", |
| 45 | + expectedOutput: "🎉 Manifest has been created\n", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "should have float as type if 1 float and int are mixed", |
| 49 | + args: []string{"--config=testdata/input/flag-float-and-int.yaml"}, |
| 50 | + assertError: assert.NoError, |
| 51 | + expectedManifest: "testdata/output/flag-float-and-int.json", |
| 52 | + expectedOutput: "🎉 Manifest has been created\n", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + tmpManifest, err := os.CreateTemp("", "temp") |
| 59 | + os.Remove(tmpManifest.Name()) |
| 60 | + |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + redirectionStd, err := os.CreateTemp("", "temp") |
| 64 | + require.NoError(t, err) |
| 65 | + defer func() { |
| 66 | + _ = os.Remove(redirectionStd.Name()) |
| 67 | + }() |
| 68 | + |
| 69 | + tt.args = append(tt.args, "--flag_manifest_destination", tmpManifest.Name()) |
| 70 | + |
| 71 | + cmd := manifest.NewManifestCmd() |
| 72 | + cmd.SetErr(redirectionStd) |
| 73 | + cmd.SetOut(redirectionStd) |
| 74 | + cmd.SetArgs(tt.args) |
| 75 | + err = cmd.Execute() |
| 76 | + |
| 77 | + tt.assertError(t, err) |
| 78 | + |
| 79 | + output, err := os.ReadFile(redirectionStd.Name()) |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Equal(t, tt.expectedOutput, string(output), "output is not expected") |
| 82 | + |
| 83 | + if tt.expectedManifest != "" { |
| 84 | + wantManifest, err := os.ReadFile(tt.expectedManifest) |
| 85 | + assert.NoError(t, err) |
| 86 | + gotManifest, err := os.ReadFile(tmpManifest.Name()) |
| 87 | + assert.NoError(t, err) |
| 88 | + assert.Equal(t, string(wantManifest), string(gotManifest), "manifest is not expected") |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments