(t *testing.T)
| 47 | } |
| 48 | |
| 49 | func TestFetchSourcesFromPolicy(t *testing.T) { |
| 50 | fs := afero.NewMemMapFs() |
| 51 | ctx := utils.WithFS(context.Background(), fs) |
| 52 | |
| 53 | downloader := mockDownloader{} |
| 54 | ctx = context.WithValue(ctx, source.DownloaderFuncKey, &downloader) |
| 55 | |
| 56 | createDir := func(args mock.Arguments) { |
| 57 | dir := args.String(0) |
| 58 | |
| 59 | if err := fs.MkdirAll(dir, 0o755); err != nil { |
| 60 | panic(err) |
| 61 | } |
| 62 | if err := afero.WriteFile(fs, fmt.Sprintf("%s/foo.rego", args.String(0)), []byte("package foo\n\nbar = 1"), 0o644); err != nil { |
| 63 | panic(err) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | downloader.On("Download", mock.Anything, "one", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir) |
| 68 | downloader.On("Download", mock.Anything, "two", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir) |
| 69 | downloader.On("Download", mock.Anything, "three", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir) |
| 70 | |
| 71 | inspectPolicyCmd := inspectPolicyCmd() |
| 72 | cmd := setUpCobra(inspectPolicyCmd) |
| 73 | cmd.SetContext(ctx) |
| 74 | buffy := bytes.Buffer{} |
| 75 | cmd.SetOut(&buffy) |
| 76 | |
| 77 | cmd.SetArgs([]string{ |
| 78 | "inspect", |
| 79 | "policy", |
| 80 | "--policy", |
| 81 | `{"sources":[{"policy":["one","two"]},{"policy":["three"]}]}`, |
| 82 | }) |
| 83 | |
| 84 | err := cmd.Execute() |
| 85 | assert.NoError(t, err) |
| 86 | |
| 87 | assert.Equal(t, "[one,two,three]", inspectPolicyCmd.Flag("source").Value.String()) |
| 88 | assert.Equal(t, "# Source: file::one\n\n# Source: file::three\n\n# Source: file::two\n\n", buffy.String()) |
| 89 | } |
| 90 | |
| 91 | func TestFetchSources(t *testing.T) { |
| 92 | fs := afero.NewMemMapFs() |
nothing calls this directly
no test coverage detected