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

Fix merge integration test #3552

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 11 additions & 1 deletion pkg/buildscript/buildscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type BuildScript struct {

project string
atTime *time.Time

// solveAtTime is the original at_time found in the solve node.
// This is used to support the legacy use case where the at_time
// is an actual time stamp and not a reference to the $at_time variable.
solveAtTime *time.Time
}

func init() {
Expand Down Expand Up @@ -53,7 +58,12 @@ func (b *BuildScript) SetProject(url string) {
}

func (b *BuildScript) AtTime() *time.Time {
return b.atTime
// If the atTime is set, prefer that over the
// legacy solveAtTime.
if b.atTime != nil {
return b.atTime
}
return b.solveAtTime
}

func (b *BuildScript) SetAtTime(t time.Time) {
Expand Down
56 changes: 56 additions & 0 deletions pkg/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package buildscript

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/ActiveState/cli/internal/environment"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -94,6 +98,58 @@ func TestRoundTripFromBuildExpression(t *testing.T) {
require.Equal(t, string(basicBuildExpression), string(data))
}

func TestRoundTripFromBuildExpressionWithLegacyAtTime(t *testing.T) {
wd, err := environment.GetRootPath()
require.NoError(t, err)

initialTimeStamp := "2024-10-15T16:37:06.260Z"
updatedTimeStamp := "2024-10-15T16:37:06.261Z"

data, err := fileutils.ReadFile(filepath.Join(wd, "pkg", "buildscript", "testdata", "buildexpression-roundtrip-legacy.json"))
require.NoError(t, err)

// The initial build expression does not use the new at_time format
assert.NotContains(t, string(data), "$at_time")
assert.Contains(t, string(data), initialTimeStamp)

script := New()
require.NoError(t, script.UnmarshalBuildExpression(data))

// Ensure that legacy at_time is preserved in the buildscript.
atTime := script.AtTime()
require.NotNil(t, atTime)
require.Equal(t, initialTimeStamp, atTime.Format(strfmt.RFC3339Millis))

data, err = script.MarshalBuildExpression()
require.NoError(t, err)

// When the build expression is unmarshalled it should now use the new at_time format
assert.Contains(t, string(data), "$at_time")
assert.NotContains(t, string(data), initialTimeStamp)

// Update the time in the build script
updatedTime, err := time.Parse(strfmt.RFC3339Millis, updatedTimeStamp)
require.NoError(t, err)
script.SetAtTime(updatedTime)

// The updated time should be reflected in the build script
require.Equal(t, updatedTime, *script.AtTime())

data, err = script.Marshal()
require.NoError(t, err)

// The marshalled build script should now contain the updated time
// in the Time block at the top of the script.
assert.Contains(t, string(data), updatedTimeStamp)
assert.NotContains(t, string(data), fmt.Sprintf("Time: %s", initialTimeStamp))

data, err = script.MarshalBuildExpression()
require.NoError(t, err)

// The build expression representation should now use the new at_time format
assert.Contains(t, string(data), "$at_time")
}

// TestExpressionToScript tests that creating a build script from a given Platform build expression
// and at time produces the expected result.
func TestExpressionToScript(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/buildscript/testdata/buildexpression-roundtrip-legacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"let": {
"sources": {
"solve": {
"at_time": "2024-10-15T16:37:06.260Z",
"solver_version": null,
"platforms": [
"46a5b48f-226a-4696-9746-ba4d50d661c2",
"78977bc8-0f32-519d-80f3-9043f059398c",
"7c998ec2-7491-4e75-be4d-8885800ef5f2"
],
"requirements": [
{
"name": "community_artifact",
"namespace": "internal"
},
{
"name": "python",
"namespace": "language",
"version_requirements": [
{
"comparator": "eq",
"version": "3.11.10"
}
]
}
]
}
},
"artifacts": {
"community_artifacts": {
"src": "$sources"
}
},
"runtime": {
"state_tool_artifacts": {
"src": "$artifacts",
"build_flags": []
}
},
"in": "$runtime"
}
}
2 changes: 1 addition & 1 deletion pkg/buildscript/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ func Unmarshal(data []byte) (*BuildScript, error) {
atTime = ptr.To(time.Time(atTimeStr))
}

return &BuildScript{raw, project, atTime}, nil
return &BuildScript{raw, project, atTime, nil}, nil
}
5 changes: 3 additions & 2 deletions pkg/buildscript/unmarshal_buildexpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"
"time"

"github.com/go-openapi/strfmt"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/sliceutils"
"github.com/go-openapi/strfmt"
)

// At this time, there is no way to ask the Platform for an empty build expression.
Expand Down Expand Up @@ -76,7 +76,8 @@ func (b *BuildScript) UnmarshalBuildExpression(data []byte) error {
}
atTimeNode.Str = nil
atTimeNode.Ident = ptr.To("TIME")
b.atTime = ptr.To(time.Time(atTime))
// Preserve the original at_time found in the solve node.
b.solveAtTime = ptr.To(time.Time(atTime))
} else if atTimeNode.Ident != nil && *atTimeNode.Ident == "at_time" {
atTimeNode.Ident = ptr.To("TIME")
}
Expand Down
Loading