-
Notifications
You must be signed in to change notification settings - Fork 137
tests: Add unit tests for project create command #694
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
db92ef2
e41374f
d6c57c6
40f7cde
48f1f4b
954a418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,8 @@ package project | |||||||||
|
|
||||||||||
| import ( | ||||||||||
| "fmt" | ||||||||||
| "io" | ||||||||||
| "os" | ||||||||||
|
|
||||||||||
| "github.com/goharbor/harbor-cli/pkg/api" | ||||||||||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||||||||||
|
|
@@ -24,52 +26,68 @@ import ( | |||||||||
| ) | ||||||||||
|
|
||||||||||
| // CreateProjectCommand creates a new `harbor create project` command | ||||||||||
| func CreateProjectCommand() *cobra.Command { | ||||||||||
| var opts create.CreateView | ||||||||||
| type ProjectCreator interface { | ||||||||||
| CreateProject(opts create.CreateView) error | ||||||||||
| FillProjectView(createView *create.CreateView) error | ||||||||||
| } | ||||||||||
| type DefaultProjectCreator struct{} | ||||||||||
|
||||||||||
|
|
||||||||||
| cmd := &cobra.Command{ | ||||||||||
| Use: "create [project name]", | ||||||||||
| Short: "create project", | ||||||||||
| Args: cobra.MaximumNArgs(1), | ||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||
| var err error | ||||||||||
| var ProjectName string | ||||||||||
| if len(args) > 0 { | ||||||||||
| opts.ProjectName = args[0] | ||||||||||
| } | ||||||||||
| func (d *DefaultProjectCreator) CreateProject(opts create.CreateView) error { | ||||||||||
| return api.CreateProject(opts) | ||||||||||
| } | ||||||||||
| func (d *DefaultProjectCreator) FillProjectView(createView *create.CreateView) error { | ||||||||||
| return create.CreateProjectView(createView) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if opts.ProxyCache && opts.RegistryID == "" { | ||||||||||
| return fmt.Errorf("proxy cache selected but no registry ID provided. Use --registry-id") | ||||||||||
| } | ||||||||||
| func CreateProject(w io.Writer, projectCreator ProjectCreator, opts *create.CreateView, args []string) error { | ||||||||||
| var ProjectName string | ||||||||||
|
||||||||||
| var createView *create.CreateView | ||||||||||
| if len(args) > 0 { | ||||||||||
| opts.ProjectName = args[0] | ||||||||||
|
Comment on lines
41
to
45
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| if !opts.ProxyCache && opts.RegistryID != "" { | ||||||||||
| return fmt.Errorf("registry ID should only be provided when proxy-cache is enabled") | ||||||||||
| } | ||||||||||
| if opts.ProxyCache && opts.RegistryID == "" { | ||||||||||
| return fmt.Errorf("proxy cache selected but no registry ID provided. Use --registry-id") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if opts.ProjectName != "" && opts.StorageLimit != "" { | ||||||||||
| log.Debug("Attempting to create project using flags...") | ||||||||||
| err = api.CreateProject(opts) | ||||||||||
| ProjectName = opts.ProjectName | ||||||||||
| } else { | ||||||||||
| log.Debug("Switching to interactive view...") | ||||||||||
| createView := &create.CreateView{ | ||||||||||
| ProjectName: opts.ProjectName, | ||||||||||
| Public: opts.Public, | ||||||||||
| RegistryID: opts.RegistryID, | ||||||||||
| StorageLimit: opts.StorageLimit, | ||||||||||
| ProxyCache: opts.ProxyCache, | ||||||||||
| } | ||||||||||
| if !opts.ProxyCache && opts.RegistryID != "" { | ||||||||||
| return fmt.Errorf("registry ID should only be provided when proxy-cache is enabled") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| err = createProjectView(createView) | ||||||||||
| ProjectName = createView.ProjectName | ||||||||||
| } | ||||||||||
| if opts.ProjectName == "" || opts.StorageLimit == "" { | ||||||||||
| log.Debug("Switching to interactive view...") | ||||||||||
| createView = &create.CreateView{ | ||||||||||
| ProjectName: opts.ProjectName, | ||||||||||
| Public: opts.Public, | ||||||||||
| RegistryID: opts.RegistryID, | ||||||||||
| StorageLimit: opts.StorageLimit, | ||||||||||
| ProxyCache: opts.ProxyCache, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("failed to create project: %v", utils.ParseHarborErrorMsg(err)) | ||||||||||
| } | ||||||||||
| err := fillCreateView(projectCreator, createView) | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("Failed to get the required params to create project:%w", err) | ||||||||||
|
||||||||||
| return fmt.Errorf("Failed to get the required params to create project:%w", err) | |
| return fmt.Errorf("failed to get the required params to create project: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
Copilot
AI
Feb 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message has inconsistent capitalization and formatting. It starts with uppercase "Failed" while other error messages in the same function (lines 50, 54, 77) start with lowercase. Additionally, there should be a space after the colon before %w. Go convention is to use lowercase for error messages unless they start with a proper noun or acronym. Change to: "failed to get the required params to create project: %w"
| return fmt.Errorf("Failed to get the required params to create project:%w", err) | |
| return fmt.Errorf("failed to get the required params to create project: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved above
Outdated
Copilot
AI
Feb 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name "ProjectName" uses PascalCase instead of camelCase. Go convention for local variables is to use camelCase. This should be renamed to "projectName" for consistency with Go naming conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
Copilot
AI
Feb 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable declaration can be simplified. The variable ProjectName is declared at line 43 but only assigned at line 75 and used at line 79. Consider removing the declaration at line 43 and directly using createView.ProjectName in the fmt.Fprintf call at line 79, eliminating the unnecessary intermediate variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont think this matters much
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment "CreateProjectCommand creates a new
harbor create projectcommand" is misplaced. It appears above the ProjectCreator interface definition but actually describes the CreateProjectCommand function further down. Move this comment to line 82 above the CreateProjectCommand function, or update it to describe the ProjectCreator interface instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved above