Enable absolute and relative parent paths for pulumi main (#6734)

This commit is contained in:
Evan Boyle 2021-04-08 21:39:52 -07:00 committed by GitHub
parent e460ab7be5
commit 15120b99e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 61 additions and 28 deletions

View file

@ -5,6 +5,9 @@
### Improvements
- [cli] Enable absolute and relative parent paths for pulumi main
[#6734](https://github.com/pulumi/pulumi/pull/6734)
- [automation/python] Update pulumi python docker image to python 3.9
[#6706](https://github.com/pulumi/pulumi/pull/6706)

View file

@ -16,9 +16,7 @@ package engine
import (
"os"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
@ -50,16 +48,11 @@ func getPwdMain(root, main string) (string, string, error) {
if main == "" {
main = "."
} else {
// The path must be relative from the package root.
if path.IsAbs(main) {
return "", "", errors.New("project 'main' must be a relative path")
}
// Check that main is a subdirectory.
cleanPwd := filepath.Clean(pwd)
main = filepath.Clean(filepath.Join(cleanPwd, main))
if !strings.HasPrefix(main, cleanPwd) {
return "", "", errors.New("project 'main' must be a subfolder")
// The path can be relative from the package root.
if !filepath.IsAbs(main) {
cleanPwd := filepath.Clean(pwd)
main = filepath.Clean(filepath.Join(cleanPwd, main))
}
// So that any relative paths inside of the program are correct, we still need to pass the pwd

View file

@ -92,7 +92,7 @@ func TestProjectMain(t *testing.T) {
}
integration.ProgramTest(t, &test)
t.Run("Error_AbsolutePath", func(t *testing.T) {
t.Run("AbsolutePath", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
@ -100,15 +100,27 @@ func TestProjectMain(t *testing.T) {
}
}()
e.ImportDirectory("project_main_abs")
// write a new Pulumi.yaml using the absolute path of the environment as "main"
yamlPath := filepath.Join(e.RootPath, "Pulumi.yaml")
absYamlContents := fmt.Sprintf(
"name: project_main_abs\ndescription: A program with an absolute entry point\nruntime: nodejs\nmain: %s\n",
e.RootPath,
)
t.Logf("writing new Pulumi.yaml: \npath: %s\ncontents:%s", yamlPath, absYamlContents)
if err := os.WriteFile(yamlPath, []byte(absYamlContents), 0644); err != nil {
t.Error(err)
return
}
e.RunCommand("yarn", "link", "@pulumi/pulumi")
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "main-abs")
stdout, stderr := e.RunCommandExpectError("pulumi", "up", "--non-interactive", "--yes", "--skip-preview")
assert.Equal(t, "Updating (main-abs):\n \n", stdout)
assert.Contains(t, stderr, "project 'main' must be a relative path")
e.RunCommand("pulumi", "preview")
e.RunCommand("pulumi", "stack", "rm", "--yes")
})
t.Run("Error_ParentFolder", func(t *testing.T) {
t.Run("ParentFolder", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
@ -116,11 +128,15 @@ func TestProjectMain(t *testing.T) {
}
}()
e.ImportDirectory("project_main_parent")
// yarn link first
e.RunCommand("yarn", "link", "@pulumi/pulumi")
// then virtually change directory to the location of the nested Pulumi.yaml
e.CWD = filepath.Join(e.RootPath, "foo", "bar")
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "main-parent")
stdout, stderr := e.RunCommandExpectError("pulumi", "up", "--non-interactive", "--yes", "--skip-preview")
assert.Equal(t, "Updating (main-parent):\n \n", stdout)
assert.Contains(t, stderr, "project 'main' must be a subfolder")
e.RunCommand("pulumi", "preview")
e.RunCommand("pulumi", "stack", "rm", "--yes")
})
}

View file

@ -0,0 +1,3 @@
/bin/
/node_modules/

View file

@ -1,5 +0,0 @@
name: project_main_abs
description: A program with an absolute entry point (should fail)
runtime: nodejs
main: /user/bin

View file

@ -0,0 +1,3 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
console.log("So much abs main");

View file

@ -0,0 +1,7 @@
{
"name": "project_main",
"license": "Apache-2.0",
"devDependencies": {
"typescript": "^2.5.3"
}
}

View file

@ -1,4 +0,0 @@
name: project_main_parent
description: A program with an entry point in a parent folder (should fail)
runtime: nodejs
main: ./foo/../../bar

View file

@ -0,0 +1,3 @@
/bin/
/node_modules/

View file

@ -0,0 +1,4 @@
name: project_main_parent
description: A program with an entry point in a parent folder
runtime: nodejs
main: ../../foo

View file

@ -0,0 +1,3 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
console.log("So much abs main");

View file

@ -0,0 +1,7 @@
{
"name": "project_main",
"license": "Apache-2.0",
"devDependencies": {
"typescript": "^2.5.3"
}
}