Skip to content

CLI : added unit test files for project and project role#6418

Open
DharunMR wants to merge 1 commit into
mindersec:mainfrom
DharunMR:test-project-cmd
Open

CLI : added unit test files for project and project role#6418
DharunMR wants to merge 1 commit into
mindersec:mainfrom
DharunMR:test-project-cmd

Conversation

@DharunMR
Copy link
Copy Markdown
Contributor

@DharunMR DharunMR commented Apr 25, 2026

Description

Continuing the pattern in #6386:

This PR introduces a comprehensive refactor of the cmd/cli/app/project and cmd/cli/app/project/role package to modernize its testing architecture, improve separation of concerns. Stripped out manual ctx and grpc.ClientConn arguments from command execution functions to align with standard Cobra RunE signatures. Implemented the "backpack" pattern using cmd.SetContext(ctx) and cli.WithRPCClient to inject mock clients cleanly during testing.

🧪 How to Test

  1. Run standard tests: go test ./... (Should PASS)
  2. Run test if any changes made in cmd code then add -update flag: go test ./cmd/cli/app/project/ -update and go test ./cmd/cli/app/project/role -update

Signed-off-by: DharunMR <maddharun56@gmail.com>
@DharunMR DharunMR requested a review from a team as a code owner April 25, 2026 03:09
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage is 60.387%DharunMR:test-project-cmd into mindersec:main. No base build found for mindersec:main.

Copy link
Copy Markdown
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

See also my comment on #6420 about having a generic cmd -> grpc client function.

@DharunMR
Copy link
Copy Markdown
Contributor Author

See also my comment on #6420 about having a generic cmd -> grpc client function.

i think this pr needs to wait until #6420 got merged, so that i dont double do code changes and simply rebase

@evankanderson
Copy link
Copy Markdown
Member

#6420 got merged, but we probably need to address #6417 (comment) -- coordinate with @sachin9058 on who's going to fix it. (TL;DR - Google deliberately makes protojson marshalling non-stable by inserting semantically meaningless spaces based on the build info.)

@sachin9058
Copy link
Copy Markdown
Contributor

Thanks for the heads up!

I’ve been working on this in #6417 the issue comes from protojson output not being stable across environments, which causes flaky golden tests due to spacing differences.

In my PR, I removed JSON golden tests and kept table/yaml outputs to avoid this instability.

Happy to coordinate here we can either follow the same approach (skip JSON golden tests) or decide on a shared normalization strategy if we want to keep them.

@evankanderson
Copy link
Copy Markdown
Member

@sachin9058 filed #6430 to track the JSON golden test problem.

Copy link
Copy Markdown
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

Sorry for missing the comments on this earlier in the protojson-is-nondeterministic excitement.

Comment on lines +3 to +8
{
"displayName": "Mock Admin",
"subject": "admin-subject-123",
"role": "admin",
"project": "00000000-0000-0000-0000-000000000001"
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a github action "user" here?

Suggested change
{
"displayName": "Mock Admin",
"subject": "admin-subject-123",
"role": "admin",
"project": "00000000-0000-0000-0000-000000000001"
}
{
"displayName": "Mock Admin",
"subject": "admin-subject-123",
"role": "admin",
"project": "00000000-0000-0000-0000-000000000001"
},
{
"displayName": "githubactions/repo:myorg/myrepo:ref:refs/heads/main",
"subject": "githubactions/repo+myorg/myrepo+ref+refs/heads/main",
"role": "editor",
"project": "00000000-0000-0000-0000-000000000001"
},

Comment on lines +31 to +32
// GetPermissionsClient is a helper to get the PermissionsServiceClient, supporting mocks
func GetPermissionsClient(cmd *cobra.Command) (minderv1.PermissionsServiceClient, func(), error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Switch this to cli.GetGRPCClient?

// DenyCommand is the command for removing a role assignment from a project
func DenyCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewPermissionsServiceClient(conn)
func DenyCommand(cmd *cobra.Command, _ []string) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder why this function is exported. Can you switch it to denyCommand?

// GrantCommand is the command for granting roles
func GrantCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewPermissionsServiceClient(conn)
func GrantCommand(cmd *cobra.Command, _ []string) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditto on unexporting this unless we need it exported (you'll also need to fix line 32)

Suggested change
func GrantCommand(cmd *cobra.Command, _ []string) error {
func grantCommand(cmd *cobra.Command, _ []string) error {

t.Render()
if len(resp.Invitations) > 0 {
t := initializeTableForGrantListInvitations(cmd.OutOrStdout())
t2 := initializeTableForGrantListInvitations(cmd.OutOrStdout())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why change this name? I see that this t is shadowing the one in the outer scope, but that doesn't seem to be a problem.

// listCommand is the command for listing projects
func createCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewProjectsServiceClient(conn)
// createCommand is the command for listing projects
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// createCommand is the command for listing projects
// createCommand is the command for creating a new project

client := minderv1.NewProjectsServiceClient(conn)
// createCommand is the command for listing projects
func createCommand(cmd *cobra.Command, _ []string) error {
client, cleanup, err := GetProjectsClient(cmd)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Switch to cli.GetGRPCClient

// listCommand is the command for listing projects
func deleteCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error {
client := minderv1.NewProjectsServiceClient(conn)
// deleteCommand is the command for listing projects
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// deleteCommand is the command for listing projects
// deleteCommand is the command to permanently delete a project

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.

4 participants