Skip to content
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

End to end tests and fixes for state publish #2679

Open
wants to merge 2 commits into
base: DX-1754
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cmd/state-offline-installer/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (r *runner) configureEnvironment(path string, namespace *project.Namespaced
}

if rt.GOOS == "windows" {
contents, err := assets.ReadFileBytes("scripts/setenv.bat")
contents, err := assets.ReadFile("scripts/setenv.bat")
if err != nil {
return errs.Wrap(err, "Error reading file bytes")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/app/app_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (a *App) Path() string {
}

func (a *App) createIcon(path string) error {
icon, err := assets.ReadFileBytes(a.options.IconFileSource)
icon, err := assets.ReadFile(a.options.IconFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand All @@ -96,7 +96,7 @@ func (a *App) createIcon(path string) error {

func (a *App) createExecFile(base string) error {
path := filepath.Join(base, "Contents", "MacOS")
asset, err := assets.ReadFileBytes(execFileSource)
asset, err := assets.ReadFile(execFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (a *App) createExecFile(base string) error {

func (a *App) createInfoFile(base string) error {
path := filepath.Join(base, "Contents")
asset, err := assets.ReadFileBytes(launchFileSource)
asset, err := assets.ReadFile(launchFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
8 changes: 6 additions & 2 deletions internal/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func (a *AssetsFS) ReadDir(name string) ([]iofs.DirEntry, error) {
return a.fs.ReadDir("contents/" + name)
}

// ReadFileBytes reads and returns bytes from the given file in this package's embedded assets.
func (a *AssetsFS) ReadFile(filename string) ([]byte, error) {
return a.fs.ReadFile("contents/" + filename)
}

// ReadFile reads and returns bytes from the given file in this package's embedded assets.
// Filenames should use forward slashes, not `filepath.Join()`, because go:embed requires '/'.
func ReadFileBytes(filename string) ([]byte, error) {
func ReadFile(filename string) ([]byte, error) {
return fs.ReadFile("contents/" + filename)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/captain/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func (cmd *Command) Usage() error {
"childCommands": childCommands,
})

contents, err := assets.ReadFileBytes("usage.tpl")
contents, err := assets.ReadFile("usage.tpl")
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
38 changes: 21 additions & 17 deletions internal/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fileutils
import (
"bytes"
"crypto/sha256"
"embed"
"encoding/hex"
"errors"
"fmt"
Expand All @@ -17,7 +18,6 @@ import (
"time"
"unicode"

"github.com/ActiveState/cli/internal/assets"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
Expand Down Expand Up @@ -268,20 +268,6 @@ func CopyFile(src, target string) error {
return nil
}

func CopyAsset(assetName, dest string) error {
asset, err := assets.ReadFileBytes(assetName)
if err != nil {
return errs.Wrap(err, "Asset %s failed", assetName)
}

err = ioutil.WriteFile(dest, asset, 0644)
if err != nil {
return errs.Wrap(err, "ioutil.WriteFile %s failed", dest)
}

return nil
}

func CopyMultipleFiles(files map[string]string) error {
for src, target := range files {
err := CopyFile(src, target)
Expand Down Expand Up @@ -703,6 +689,7 @@ func WriteTempFileToDir(dir, pattern string, data []byte, perm os.FileMode) (str

type DirReader interface {
ReadDir(string) ([]os.DirEntry, error)
ReadFile(string) ([]byte, error)
}

func CopyFilesDirReader(reader DirReader, src, dst, placeholderFileName string) error {
Expand Down Expand Up @@ -736,9 +723,14 @@ func CopyFilesDirReader(reader DirReader, src, dst, placeholderFileName string)
continue
}

err := CopyAsset(srcPath, destPath)
f, err := reader.ReadFile(srcPath)
if err != nil {
return errs.Wrap(err, "CopyFile %s:%s failed", srcPath, destPath)
return errs.Wrap(err, "Read file %s failed", srcPath)
}

err = os.WriteFile(destPath, f, 0644)
if err != nil {
return errs.Wrap(err, "Write file %s failed", destPath)
}
}
}
Expand Down Expand Up @@ -1184,3 +1176,15 @@ func globPath(path string) string {
}
return result
}

type EmbedFS struct {
fs embed.FS
}

func NewEmbedFS(fs embed.FS) *EmbedFS {
return &EmbedFS{fs: fs}
}

func (a *EmbedFS) ReadDir(name string) ([]fs.DirEntry, error) {
return a.fs.ReadDir(name)
}
6 changes: 6 additions & 0 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ err_project_no_languages:
other: Your project does not have any languages configured
err_add_commit:
other: "Error occurred while trying to create a commit: {{.V0}}"
err_add_commit_initial:
other: "Error occurred while trying to create initial commit: {{.V0}}"
err_update_branch:
other: "Error occurred while trying to update a branch: {{.V0}}"
err_get_commit_history:
Expand Down Expand Up @@ -1186,6 +1188,10 @@ package_added:
other: "Package added: [NOTICE]{{.V0}}[/RESET]"
package_version_added:
other: "Package added: [NOTICE]{{.V0}}@{{.V1}}[/RESET]"
raw_added:
other: "Dependency added: [NOTICE]{{.V0}}[/RESET]"
raw_version_added:
other: "Dependency added: [NOTICE]{{.V0}}@{{.V1}}[/RESET]"
bundle_added:
other: "[SUCCESS]✔ {{.V0}} Bundle successfully installed![/RESET]"
bundle_version_added:
Expand Down
2 changes: 1 addition & 1 deletion internal/notify/notify_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Send(title, message, actionName, actionLink string) error {
iconBytes, err := assets.ReadFileBytes("icon.png")
iconBytes, err := assets.ReadFile("icon.png")
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/osutils/autostart/autostart_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func enable(exec string, opts Options) error {
return errs.Wrap(err, "Could not get launch file")
}

asset, err := assets.ReadFileBytes(autostartFileSource)
asset, err := assets.ReadFile(autostartFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/osutils/autostart/autostart_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func enable(exec string, opts Options) error {
return errs.Wrap(err, "Could not create shortcut")
}

icon, err := assets.ReadFileBytes(opts.IconFileSource)
icon, err := assets.ReadFile(opts.IconFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
9 changes: 7 additions & 2 deletions internal/runbits/requirements/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ func (r *RequirementOperation) ExecuteRequirementOperation(
)

if !hasParentCommit {
languageFromNs := model.LanguageFromNamespace(ns.String())
var languageFromNs string
if ns.Type() != model.NamespaceRaw {
languageFromNs = model.LanguageFromNamespace(ns.String())
}
parentCommitID, err = model.CommitInitial(model.HostPlatform, languageFromNs, langVersion)
if err != nil {
return locale.WrapError(err, "err_install_no_project_commit", "Could not create initial commit for new project")
Expand Down Expand Up @@ -259,7 +262,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(
bp := model.NewBuildPlannerModel(r.Auth)
commitID, err := bp.StageCommit(params)
if err != nil {
return locale.WrapError(err, "err_package_save_and_build", "Error occurred while trying to create a commit")
return locale.WrapError(err, "err_package_save_and_build", "Error occurred while trying to stage a commit")
}

orderChanged := !hasParentCommit
Expand All @@ -283,6 +286,8 @@ func (r *RequirementOperation) ExecuteRequirementOperation(
trigger = target.TriggerPackage
case model.NamespacePlatform:
trigger = target.TriggerPlatform
case model.NamespaceRaw:
trigger = target.TriggerCustom
default:
return errs.Wrap(err, "Unsupported namespace type: %s", ns.Type().String())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/clean/run_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func removeInstall(logFile string, params *UninstallParams, cfg *config.Instance
func removePaths(logFile string, paths ...string) error {
logging.Debug("Removing paths: %v", paths)
scriptName := "removePaths"
scriptBlock, err := assets.ReadFileBytes(fmt.Sprintf("scripts/%s.bat", scriptName))
scriptBlock, err := assets.ReadFile(fmt.Sprintf("scripts/%s.bat", scriptName))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (d *Deploy) install(rtTarget setup.Targeter) (rerr error) {
_, _ = rti.Env(false, false)

if rt.GOOS == "windows" {
contents, err := assets.ReadFileBytes("scripts/setenv.bat")
contents, err := assets.ReadFile("scripts/setenv.bat")
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/subshell/sscommon/rcfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func WriteRcFile(rcTemplateName string, path string, data RcIdentification, env
return err
}

tpl, err := assets.ReadFileBytes(fmt.Sprintf("shells/%s", rcTemplateName))
tpl, err := assets.ReadFile(fmt.Sprintf("shells/%s", rcTemplateName))
if err != nil {
return errs.Wrap(err, "Failed to read asset")
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func CleanRcFile(path string, data RcIdentification) error {

// SetupShellRcFile create a rc file to activate a runtime (without a project being present)
func SetupShellRcFile(rcFileName, templateName string, env map[string]string, namespace *project.Namespaced) error {
tpl, err := assets.ReadFileBytes(fmt.Sprintf("shells/%s", templateName))
tpl, err := assets.ReadFile(fmt.Sprintf("shells/%s", templateName))
if err != nil {
return errs.Wrap(err, "Failed to read asset")
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func SetupShellRcFile(rcFileName, templateName string, env map[string]string, na
// SetupProjectRcFile creates a temporary RC file that our shell is initiated from, this allows us to template the logic
// used for initialising the subshell
func SetupProjectRcFile(prj *project.Project, templateName, ext string, env map[string]string, out output.Outputer, cfg Configurable, bashifyPaths bool) (*os.File, error) {
tpl, err := assets.ReadFileBytes(fmt.Sprintf("shells/%s", templateName))
tpl, err := assets.ReadFile(fmt.Sprintf("shells/%s", templateName))
if err != nil {
return nil, errs.Wrap(err, "Failed to read asset")
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/platform/model/buildplanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewBuildPlannerModel(auth *authentication.Auth) *BuildPlanner {
logging.Debug("Using build planner at: %s", bpURL)

client := gqlclient.NewWithOpts(bpURL, 0, graphql.WithHTTPClient(&http.Client{}))
client.EnableDebugLog()

if auth.Authenticated() {
client.SetTokenProvider(auth)
Expand Down Expand Up @@ -273,10 +274,7 @@ func (bp *BuildPlanner) StageCommit(params StageCommitParams) (strfmt.UUID, erro
}
}

err = expression.UpdateTimestamp(params.TimeStamp)
if err != nil {
return "", errs.Wrap(err, "Failed to update build expression with timestamp")
}
expression.UpdateTimestamp(params.TimeStamp)

// With the updated build expression call the stage commit mutation
request := request.StageCommit(params.Owner, params.Project, params.ParentCommit, expression)
Expand Down
7 changes: 2 additions & 5 deletions pkg/platform/model/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,6 @@ func AddChangeset(parentCommitID strfmt.UUID, commitMessage string, changeset Ch
res, err := mono.New().VersionControl.AddCommit(params, authentication.ClientAuth())
if err != nil {
switch err.(type) {
case *version_control.AddCommitBadRequest,
*version_control.AddCommitConflict,
*version_control.AddCommitNotFound:
return nil, locale.WrapInputError(err, "err_add_commit", "", api.ErrorMessageFromPayload(err))
case *version_control.AddCommitForbidden:
return nil, locale.WrapInputError(err, "err_add_commit", "", locale.T("err_auth_required"))
default:
Expand Down Expand Up @@ -607,6 +603,7 @@ func CommitChangeset(parentCommitID strfmt.UUID, commitMsg string, changeset Cha

// CommitInitial creates a root commit for a new branch
func CommitInitial(hostPlatform string, langName, langVersion string) (strfmt.UUID, error) {
logging.Debug("Creating initial commit, platform: %s, language: %s@%s", hostPlatform, langName, langVersion)
platformID, err := hostPlatformToPlatformID(hostPlatform)
if err != nil {
return "", err
Expand Down Expand Up @@ -650,7 +647,7 @@ func CommitInitial(hostPlatform string, langName, langVersion string) (strfmt.UU

res, err := mono.New().VersionControl.AddCommit(params, authentication.ClientAuth())
if err != nil {
return "", locale.WrapError(err, "err_add_commit", "", api.ErrorMessageFromPayload(err))
return "", locale.WrapError(err, "err_add_commit_initial", "", api.ErrorMessageFromPayload(err))
}

return res.Payload.CommitID, nil
Expand Down
11 changes: 2 additions & 9 deletions pkg/platform/runtime/buildexpression/buildexpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,23 +679,16 @@ func (e *BuildExpression) removePlatform(platformID strfmt.UUID) error {
return nil
}

func (e *BuildExpression) UpdateTimestamp(timestamp time.Time) error {
formatted, err := time.Parse(time.RFC3339, timestamp.String())
if err != nil {
return errs.Wrap(err, "Could not parse latest timestamp")
}

func (e *BuildExpression) UpdateTimestamp(timestamp time.Time) {
for _, arg := range e.getSolveNode().Arguments {
if arg.Assignment == nil {
continue
}

if arg.Assignment.Name == "at_time" {
arg.Assignment.Value.Str = ptr.To(formatted.Format(time.RFC3339))
arg.Assignment.Value.Str = ptr.To(timestamp.Format(time.RFC3339))
}
}

return nil
}

func (e *BuildExpression) MarshalJSON() ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/platform/runtime/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
TriggerPackage Trigger = "package"
TriggerLanguage Trigger = "language"
TriggerPlatform Trigger = "platform"
TriggerCustom Trigger = "custom"
TriggerPull Trigger = "pull"
TriggerRefresh Trigger = "refresh"
TriggerReset Trigger = "reset"
Expand Down
6 changes: 3 additions & 3 deletions pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ func createCustom(params *CreateParams, lang language.Language) (*Project, error
content := params.Content
if content == "" && lang != language.Unset && lang != language.Unknown {
tplName := "activestate.yaml." + strings.TrimRight(lang.String(), "23") + ".tpl"
template, err := assets.ReadFileBytes(tplName)
template, err := assets.ReadFile(tplName)
if err != nil {
return nil, errs.Wrap(err, "Could not read asset")
}
Expand All @@ -1013,7 +1013,7 @@ func createCustom(params *CreateParams, lang language.Language) (*Project, error
}

tplName := "activestate.yaml.tpl"
tplContents, err := assets.ReadFileBytes(tplName)
tplContents, err := assets.ReadFile(tplName)
if err != nil {
return nil, errs.Wrap(err, "Could not read asset")
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func createHostFile(filePath, cachePath string) error {
}

tplName := "activestate.yaml.cache.tpl"
tplContents, err := assets.ReadFileBytes(tplName)
tplContents, err := assets.ReadFile(tplName)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
1 change: 1 addition & 0 deletions test/integration/assets/python-ingredient/example/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello")
6 changes: 6 additions & 0 deletions test/integration/assets/python-ingredient/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from setuptools import find_packages, setup
setup(name="example",
version="0.1",
author="ActiveState",
packages=find_packages(),
)
2 changes: 1 addition & 1 deletion test/integration/init_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (suite *InitIntegrationTestSuite) runInitTest(addPath bool, lang string, ex
configFilepath := filepath.Join(ts.Dirs.Work, constants.ConfigFileName)
suite.Require().FileExists(configFilepath)

templateFile, err := assets.ReadFileBytes("activestate.yaml.python.tpl")
templateFile, err := assets.ReadFile("activestate.yaml.python.tpl")
if err != nil {
panic(err.Error())
}
Expand Down
Loading