Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve binary artifact and license tests to match other checks #4032

Closed
spencerschrock opened this issue Apr 15, 2024 · 4 comments · Fixed by #4079
Closed

improve binary artifact and license tests to match other checks #4032

spencerschrock opened this issue Apr 15, 2024 · 4 comments · Fixed by #4079

Comments

@spencerschrock
Copy link
Contributor

Both Binary-Artifacts and License checks have TODOs about using a gomock repoclient.

// TODO: Use gMock instead of Localdir here.
ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)

// TODO: Use gMock instead of Localdir here.
ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)

Additionally Binary-Artifacts has is a very basic test which just check the score and not other aspects of the CheckResult (like logging).

result := BinaryArtifacts(&req)
if result.Score != tt.expected.Score {
t.Errorf("BinaryArtifacts: %v, expected %v for tests %v", result.Score, tt.expected.Score, tt.name)
}

It'd be nice to get both of these cleaned up like some of the other checks to:

  1. Use gomock
  2. use scut.ValidateTestReturn

For example like dangerous_workflow_test.go

dl := scut.TestDetailLogger{}
ctrl := gomock.NewController(t)
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
mockRepoClient.EXPECT().ListFiles(gomock.Any()).Return(tt.workflowPaths, nil)
mockRepoClient.EXPECT().GetFileReader(gomock.Any()).DoAndReturn(func(file string) (io.ReadCloser, error) {
return os.Open("./testdata/" + file)
}).AnyTimes()
req := &checker.CheckRequest{
Ctx: context.Background(),
RepoClient: mockRepoClient,
Dlogger: &dl,
}
result := DangerousWorkflow(req)
scut.ValidateTestReturn(t, tt.name, &tt.expected, &result, &dl)

@seelder
Copy link
Contributor

seelder commented May 2, 2024

I think maybe I can work on this one? I haven't used gomock before, but it looks like BinaryArtifacts uses a lot of the same functions as DangerousWorkflow so hopefully I can figure it out.

@spencerschrock
Copy link
Contributor Author

DangerousWorkflow should be a good template, but happy to answer any questions as needed

@seelder
Copy link
Contributor

seelder commented May 3, 2024

(@spencerschrock) - currently the ListLicenses function for the localDirRepo client throws an Unsupported Feature error, which is then handled by the raw license check. Options I see include

  1. create a mock that just throws an UnsupportedFeature error, similar to the existing functionality (I already have a working version of this, I think)

  2. create a mock that actually returns something from ListLicenses, however, that would replicate a lot of the existing functionality for handling these cases in the check itself

  3. Testing both 1 and 2 using different test cases with different mocks (adding a couple test cases to do so), although it seems like the difference in functionality should be tested in the raw/checks/license_test rather than here, so this might involve some other test design decisions

My questions are along the lines of - Is the functionality in the license check that handles an unsupportedFeature for listLicenses intended to be permanent? Are any of the options above preferable given the test designs for the project?

I am probably over-thinking this. If so and if option 1 is fine, I can go ahead and submit a PR.

Thanks!

@spencerschrock
Copy link
Contributor Author

Option (1) sounds reasonable and a more direct translation of the existing tests, and would be a good first PR.

Depending on how much you want to do, creating a separate test which uses ListLicenses (effectively option 3) also sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants