Skip to content
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
8 changes: 8 additions & 0 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2011,3 +2011,11 @@ warning_macos_bash:
operating_message:
other: |
Operating on project [ACTIONABLE]{{.V0}}[/RESET], located at [ACTIONABLE]{{.V1}}[/RESET].
pjfile_deprecation_msg:
other: |
Your activestate.yaml located at [ACTIONABLE]{{.V0}}[/RESET] appears to be using deprecated keys. Please update the following keys:
{{.V1}}
To find out how to update these please read the deprecation information at:
[ACTIONABLE]{{.V2}}[/RESET]
pjfile_deprecation_entry:
other: " - '[ACTIONABLE]{{.V0}}[/RESET]' located at byte [ACTIONABLE]{{.V1}}[/RESET]"
18 changes: 1 addition & 17 deletions internal/testhelpers/e2e/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ import (
"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/project"
"github.com/ActiveState/cli/pkg/projectfile"
"github.com/ActiveState/termtest"
"github.com/ActiveState/termtest/expect"
"github.com/go-openapi/strfmt"
"github.com/google/uuid"
"github.com/phayes/permbits"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

// Session represents an end-to-end testing session during which several console process can be spawned and tested
Expand Down Expand Up @@ -293,21 +291,7 @@ func (s *Session) SpawnCmdWithOpts(exe string, opts ...SpawnOptions) *termtest.C
// provided contents and saves the output to an as.y file within the named
// directory.
func (s *Session) PrepareActiveStateYAML(contents string) {
msg := "cannot setup activestate.yaml file"

contents = strings.TrimSpace(contents)
projectFile := &projectfile.Project{}

err := yaml.Unmarshal([]byte(contents), projectFile)
require.NoError(s.t, err, msg)

cfg, err := config.New()
require.NoError(s.t, err)
defer func() { require.NoError(s.t, cfg.Close()) }()

projectFile.SetPath(filepath.Join(s.Dirs.Work, "activestate.yaml"))
err = projectFile.Save(cfg)
require.NoError(s.t, err, msg)
require.NoError(s.t, fileutils.WriteFile(filepath.Join(s.Dirs.Work, "activestate.yaml"), []byte(contents)))
}

// PrepareFile writes a file to path with contents, expecting no error
Expand Down
1 change: 1 addition & 0 deletions internal/testhelpers/tagsuite/tagsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
Platforms = "platforms"
Prepare = "prepare"
Projects = "projects"
Projectfile = "projectfile"
Pull = "pull"
Push = "push"
Python = "python"
Expand Down
29 changes: 26 additions & 3 deletions pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -47,6 +48,8 @@ var (
ProjectURLRe = regexp.MustCompile(urlProjectRegexStr)
// CommitURLRe Regex used to validate commit info /commit/someUUID
CommitURLRe = regexp.MustCompile(urlCommitRegexStr)
// deprecatedRegex covers the deprecated fields in the project file
deprecatedRegex = regexp.MustCompile(`\s*(?:constraints|platforms|languages):`)
)

type ErrorParseProject struct{ *locale.LocalizedError }
Expand Down Expand Up @@ -485,12 +488,16 @@ func parse(configFilepath string) (*Project, error) {
return nil, errs.Wrap(err, "ioutil.ReadFile %s failure", configFilepath)
}

if err := detectDeprecations(dat, configFilepath); err != nil {
return nil, errs.Wrap(err, "deprecations found")
}

project := Project{}
err = yaml.Unmarshal([]byte(dat), &project)
err2 := yaml.Unmarshal(dat, &project)
project.path = configFilepath

if err != nil {
return nil, &ErrorParseProject{locale.NewError(
if err2 != nil {
return nil, &ErrorParseProject{locale.NewInputError(
"err_project_parsed",
"Project file `{{.V1}}` could not be parsed, the parser produced the following error: {{.V0}}", err.Error(), configFilepath),
}
Expand All @@ -499,6 +506,22 @@ func parse(configFilepath string) (*Project, error) {
return &project, nil
}

func detectDeprecations(dat []byte, configFilepath string) error {
deprecations := deprecatedRegex.FindAllIndex(dat, -1)
if len(deprecations) == 0 {
return nil
}
deplist := []string{}
for _, depIdxs := range deprecations {
dep := strings.TrimSpace(strings.TrimSuffix(string(dat[depIdxs[0]:depIdxs[1]]), ":"))
deplist = append(deplist, locale.Tr("pjfile_deprecation_entry", dep, strconv.Itoa(depIdxs[0])))
}
return &ErrorParseProject{locale.NewInputError(
"pjfile_deprecation_msg",
"", configFilepath, strings.Join(deplist, "\n"), constants.DocumentationURL+"config/#deprecation"),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

}
}

// Owner returns the project namespace's organization
func (p *Project) Owner() string {
return p.parsedURL.Owner
Expand Down
73 changes: 73 additions & 0 deletions pkg/projectfile/projectfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/ActiveState/cli/internal/locale"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -433,3 +434,75 @@ func TestProject_Init(t *testing.T) {
})
}
}

func Test_detectDeprecations(t *testing.T) {
tests := []struct {
name string
dat string
wantMatchError []string
}{
{
"Constraints",
`constraints: 0`,
[]string{
locale.Tr("pjfile_deprecation_entry", "constraints", "0"),
},
},
{
"Platforms",
`platforms: 0"`,
[]string{
locale.Tr("pjfile_deprecation_entry", "platforms", "0"),
},
},
{
"Languages",
`languages: 0`,
[]string{
locale.Tr("pjfile_deprecation_entry", "languages", "0"),
},
},
{
"Mixed",
"foo: 0\nconstraints: 0\nbar: 0\nlanguages: 0, platforms: 0",
[]string{
locale.Tr("pjfile_deprecation_entry", "constraints", "6"),
locale.Tr("pjfile_deprecation_entry", "languages", "28"),
locale.Tr("pjfile_deprecation_entry", "platforms", "42"),
},
},
{
"Real world",
`project: https://platform.activestate.com/ActiveState-CLI/test?commitID=9090c128-e948-4388-8f7f-96e2c1e00d98
platforms:
- name: Linux64Label
languages:
- name: Go
constraints:
platform: Windows10Label,Linux64Label`,
[]string{
locale.Tr("pjfile_deprecation_entry", "platforms", "108"),
locale.Tr("pjfile_deprecation_entry", "languages", "142"),
locale.Tr("pjfile_deprecation_entry", "constraints", "166"),
},
},
{
"Valid",
"foo: 0\nbar: 0",
[]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := detectDeprecations([]byte(tt.dat), "activestate.yaml")
if len(tt.wantMatchError) == 0 {
assert.NoError(t, err)
return
}
require.Error(t, err)
for _, want := range tt.wantMatchError {
assert.Contains(t, err.Error(), want)
}
})
}
}
41 changes: 41 additions & 0 deletions test/integration/pjfile_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package integration

import (
"strings"
"testing"

"github.com/stretchr/testify/suite"

"github.com/ActiveState/cli/internal/testhelpers/e2e"
"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
)

type PjFileIntegrationTestSuite struct {
tagsuite.Suite
}

func (suite *PjFileIntegrationTestSuite) TestDeprecation() {
suite.OnlyRunForTags(tagsuite.Projects)
ts := e2e.New(suite.T(), false)
defer ts.Close()

ts.PrepareActiveStateYAML(strings.TrimSpace(`
project: https://platform.activestate.com/ActiveState-CLI/test?commitID=1090c128-e948-4388-8f7f-96e2c1e00d98
platforms:
- name: Linux64Label
languages:
- name: Go
constraints:
platform: Windows10Label,Linux64Label
`))

cp := ts.SpawnWithOpts(
e2e.WithArgs("scripts"),
e2e.AppendEnv("VERBOSE=true"),
)
cp.ExpectExitCode(1)
}

func TestPjFileIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(PjFileIntegrationTestSuite))
}