MCPcopy Create free account
hub / github.com/conforma/cli / TestFetchSources

Function TestFetchSources

cmd/inspect/inspect_policy_test.go:91–135  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

89}
90
91func TestFetchSources(t *testing.T) {
92 fs := afero.NewMemMapFs()
93 ctx := utils.WithFS(context.Background(), fs)
94
95 downloader := mockDownloader{}
96 ctx = context.WithValue(ctx, source.DownloaderFuncKey, &downloader)
97
98 createDir := func(args mock.Arguments) {
99 dir := args.String(0)
100
101 if err := fs.MkdirAll(dir, 0o755); err != nil {
102 panic(err)
103 }
104 if err := afero.WriteFile(fs, fmt.Sprintf("%s/foo.rego", args.String(0)), []byte("package foo\n\nbar = 1"), 0o644); err != nil {
105 panic(err)
106 }
107 }
108
109 downloader.On("Download", mock.Anything, "one", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir)
110 downloader.On("Download", mock.Anything, "two", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir)
111 downloader.On("Download", mock.Anything, "three", false).Return(&fileMetadata.FSMetadata{}, nil).Run(createDir)
112
113 inspectPolicyCmd := inspectPolicyCmd()
114 cmd := setUpCobra(inspectPolicyCmd)
115 cmd.SetContext(ctx)
116 buffy := bytes.Buffer{}
117 cmd.SetOut(&buffy)
118
119 cmd.SetArgs([]string{
120 "inspect",
121 "policy",
122 "--source",
123 "one",
124 "--source",
125 "two",
126 "--source",
127 "three",
128 })
129
130 err := cmd.Execute()
131 assert.NoError(t, err)
132
133 assert.Equal(t, "[one,two,three]", inspectPolicyCmd.Flag("source").Value.String())
134 assert.Equal(t, "# Source: file::one\n\n# Source: file::three\n\n# Source: file::two\n\n", buffy.String())
135}
136
137func TestSourcesAndPolicyCantBeBothProvided(t *testing.T) {
138 fs := afero.NewMemMapFs()

Callers

nothing calls this directly

Calls 5

WithFSFunction · 0.92
inspectPolicyCmdFunction · 0.85
setUpCobraFunction · 0.70
RunMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected