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

creating tests for files in the cmd directory #433

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Philip-21
Copy link

@Philip-21 Philip-21 commented Apr 27, 2024

Summary

Related issues

The existing implementation here contains unit test codes for different files in the cmd directory

Checklist

  • reference any related issues
  • updated docs
  • updated unit tests
  • updated UPGRADING.md
  • add appropriate tag (improvement / bug / etc)
  • ready for review

@Philip-21 Philip-21 requested a review from a team as a code owner April 27, 2024 17:43
@CLAassistant
Copy link

CLAassistant commented Apr 27, 2024

CLA assistant check
All committers have signed the CLA.

@Philip-21
Copy link
Author

Philip-21 commented Apr 27, 2024

Hi , I am Philip from Nigeria, its My first time contributing to Pomeruim, i decided to start with creating unit tests in the cli codebase to help understand and get along with pomerium technologies and its codebases, I would like to contribute more and tackle issues as they arise .

@Philip-21
Copy link
Author

i will be adding more unit tests soon before this pr can be merged

Copy link
Contributor

@kenjenkins kenjenkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Philip-21, we appreciate your willingness to contribute. Unfortunately I don't think we can merge this PR without some substantial revisions.

mockCache := new(MockCache)

//set up dir expectation for ExecCredentialsPath
mockCache.On(" ExecCredentialsPath").Return(filePath+"/tmp/cache", nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've correctly identified that ExecCredentialsPath() makes this behavior difficult to test.

However I don't think this mock object is effective at swapping out a different path. There are some early returns below, but if I change those, I see that clearAllCachedCredentials() uses the real ExecCredentialsPath() rather than this mock path.

Comment on lines 31 to 34
if err != nil {
t.Log(err)
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By ignoring this error, the test can pass without ever exercising the behavior of clearAllCachedCredentials(). In general it would probably be better to do something like require.NoError(t, err) instead (adding an import for "github.com/stretchr/testify/assert").

When running this test I found that file1.json could not be created because the tmp and cache directories did not exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i decided to use a tempdir so we wouldnt have unwanted test files generated.
If you want the files to be created i can implement that


func TestCachedCredentialPath(t *testing.T) {
//machine filepath name declared when running tests
pcPath := "/home/dell/.cache"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has the same problem as above: the behavior under test depends on the return value of ExecCredentialsPath(). We would need to make sure that this test can pass when run by GitHub Actions and when run by a developer locally.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats true, the ci checks won't pass .

@Philip-21
Copy link
Author

Hi @kenjenkins i will make adjustments where necessary, thanks for the reviews

Signed-off-by: Philip-21 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants