Skip to content

Commit

Permalink
Use YAML parser when unmarshaling buildscript checkout info.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Sep 11, 2024
1 parent 4a91dda commit ccd9554
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var basicBuildScript = []byte(
checkoutInfo(testProject, testTime) + `
checkoutInfoString(testProject, testTime) + `
runtime = state_tool_artifacts(
src = sources
)
Expand Down
16 changes: 8 additions & 8 deletions pkg/buildscript/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const mergeBTime = "2000-01-02T00:00:00.000Z"

func TestMergeAdd(t *testing.T) {
scriptA, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeATime) + `
checkoutInfoString(testProject, mergeATime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -31,7 +31,7 @@ main = runtime
require.NoError(t, err)

scriptB, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeBTime) + `
checkoutInfoString(testProject, mergeBTime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand Down Expand Up @@ -63,7 +63,7 @@ main = runtime
require.NoError(t, err)

assert.Equal(t,
checkoutInfo(testProject, mergeBTime)+`
checkoutInfoString(testProject, mergeBTime)+`
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -82,7 +82,7 @@ main = runtime`, string(v))

func TestMergeRemove(t *testing.T) {
scriptA, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeBTime) + `
checkoutInfoString(testProject, mergeBTime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -101,7 +101,7 @@ main = runtime
require.NoError(t, err)

scriptB, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeATime) + `
checkoutInfoString(testProject, mergeATime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand Down Expand Up @@ -132,7 +132,7 @@ main = runtime
require.NoError(t, err)

assert.Equal(t,
checkoutInfo(testProject, mergeBTime)+`
checkoutInfoString(testProject, mergeBTime)+`
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -150,7 +150,7 @@ main = runtime`, string(v))

func TestMergeConflict(t *testing.T) {
scriptA, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeATime) + `
checkoutInfoString(testProject, mergeATime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -167,7 +167,7 @@ main = runtime
require.NoError(t, err)

scriptB, err := Unmarshal([]byte(
checkoutInfo(testProject, mergeATime) + `
checkoutInfoString(testProject, mergeATime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand Down
6 changes: 3 additions & 3 deletions pkg/buildscript/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const testProject = "https://platform.activestate.com/org/project?branch=main&commitID=00000000-0000-0000-0000-000000000000"
const testTime = "2000-01-01T00:00:00.000Z"

func checkoutInfo(project, time string) string {
func checkoutInfoString(project, time string) string {
return "```\n" +
"Project: " + project + "\n" +
"Time: " + time + "\n" +
Expand All @@ -23,7 +23,7 @@ func checkoutInfo(project, time string) string {
var testCheckoutInfo string

func init() {
testCheckoutInfo = checkoutInfo(testProject, testTime)
testCheckoutInfo = checkoutInfoString(testProject, testTime)
}

func TestRawRepresentation(t *testing.T) {
Expand Down Expand Up @@ -175,7 +175,7 @@ main = merge(
}

func TestComplexVersions(t *testing.T) {
checkoutInfo := checkoutInfo(testProject, "2023-04-27T17:30:05.999Z")
checkoutInfo := checkoutInfoString(testProject, "2023-04-27T17:30:05.999Z")
script, err := Unmarshal([]byte(
checkoutInfo + `
runtime = solve(
Expand Down
35 changes: 21 additions & 14 deletions pkg/buildscript/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package buildscript

import (
"errors"
"regexp"
"strings"
"time"

"github.com/alecthomas/participle/v2"
"github.com/go-openapi/strfmt"
"gopkg.in/yaml.v2"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
Expand All @@ -17,7 +18,10 @@ const atTimeKey = "at_time"

var ErrOutdatedAtTime = errs.New("outdated at_time on top")

var checkoutInfoPairRegex = regexp.MustCompile(`(\w+)\s*:\s*([^\n]+)`)
type checkoutInfo struct {
Project string `yaml:"Project"`
Time string `yaml:"Time"`
}

// Unmarshal returns a structured form of the given AScript (on-disk format).
func Unmarshal(data []byte) (*BuildScript, error) {
Expand All @@ -44,19 +48,22 @@ func Unmarshal(data []byte) (*BuildScript, error) {
}

if raw.Info != nil {
for _, matches := range checkoutInfoPairRegex.FindAllStringSubmatch(*raw.Info, -1) {
key, value := matches[1], matches[2]
switch key {
case "Project":
raw.CheckoutInfo.Project = value
case "Time":
atTime, err := strfmt.ParseDateTime(value)
if err != nil {
return nil, errs.Wrap(err, "Invalid timestamp: %s", value)
}
raw.CheckoutInfo.AtTime = time.Time(atTime)
}
info := checkoutInfo{}

err := yaml.Unmarshal([]byte(strings.Trim(*raw.Info, "`\n")), &info)
if err != nil {
return nil, locale.NewExternalError(
"err_buildscript_checkoutinfo",
"Could not parse checkout information in the buildscript. The parser produced the following error: {{.V0}}", err.Error())
}

raw.CheckoutInfo.Project = info.Project

atTime, err := strfmt.ParseDateTime(info.Time)
if err != nil {
return nil, errs.Wrap(err, "Invalid timestamp: %s", info.Time)
}
raw.CheckoutInfo.AtTime = time.Time(atTime)
}

return &BuildScript{raw}, nil
Expand Down

0 comments on commit ccd9554

Please sign in to comment.