Skip to content

fix: resolve template name from working directory "." #6822

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

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ func (pf *templateUploadFlags) templateName(args []string) (string, error) {
if len(args) > 0 {
return args[0], nil
}
// Have to take absPath to resolve "." and "..".
absPath, err := filepath.Abs(pf.directory)
if err != nil {
return "", err
}
// If no name is provided, use the directory name.
return filepath.Base(pf.directory), nil
return filepath.Base(absPath), nil
}

func (r *RootCmd) templatePush() *clibase.Cmd {
Expand Down
14 changes: 12 additions & 2 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ func TestTemplatePush(t *testing.T) {
require.Equal(t, "example", templateVersions[1].Name)
})

// This test modifies the working directory.
//nolint:paralleltest
t.Run("UseWorkingDir", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
Expand All @@ -173,9 +174,18 @@ func TestTemplatePush(t *testing.T) {
r.Name = filepath.Base(source)
})

oldDir, err := os.Getwd()
require.NoError(t, err)

os.Chdir(source)

t.Cleanup(func() {
os.Chdir(oldDir)
})

// Don't pass the name of the template, it should use the
// directory of the source.
inv, root := clitest.New(t, "templates", "push", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
inv, root := clitest.New(t, "templates", "push", "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

Expand Down
3 changes: 3 additions & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ func (c *Client) TemplatesByOrganization(ctx context.Context, organizationID uui

// TemplateByName finds a template inside the organization provided with a case-insensitive name.
func (c *Client) TemplateByName(ctx context.Context, organizationID uuid.UUID, name string) (Template, error) {
if name == "" {
return Template{}, xerrors.Errorf("template name cannot be empty")
}
res, err := c.Request(ctx, http.MethodGet,
fmt.Sprintf("/api/v2/organizations/%s/templates/%s", organizationID.String(), name),
nil,
Expand Down