github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runbits/buildscript/buildscript_test.go (about)

     1  package buildscript
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/ActiveState/cli/internal/fileutils"
     9  	"github.com/ActiveState/cli/pkg/platform/runtime/buildscript"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestDiff(t *testing.T) {
    15  	script, err := buildscript.New([]byte(
    16  		`at_time = "2000-01-01T00:00:00.000Z"
    17  runtime = solve(
    18  	at_time = at_time,
    19  	platforms = [
    20  		"12345",
    21  		"67890"
    22  	],
    23  	requirements = [
    24  		Req(name = "perl", namespace = "language")
    25  	]
    26  )
    27  
    28  main = runtime`))
    29  	require.NoError(t, err)
    30  
    31  	// Modify the build script.
    32  	modifiedScript, err := buildscript.New([]byte(strings.Replace(script.String(), "12345", "77777", 1)))
    33  	require.NoError(t, err)
    34  
    35  	// Generate the difference between the modified script and the original expression.
    36  	result, err := generateDiff(modifiedScript, script)
    37  	require.NoError(t, err)
    38  	assert.Equal(t, `at_time = "2000-01-01T00:00:00.000Z"
    39  runtime = solve(
    40  	at_time = at_time,
    41  	platforms = [
    42  <<<<<<< local
    43  		"77777",
    44  =======
    45  		"12345",
    46  >>>>>>> remote
    47  		"67890"
    48  	],
    49  	requirements = [
    50  		Req(name = "perl", namespace = "language")
    51  	]
    52  )
    53  
    54  main = runtime`, result)
    55  }
    56  
    57  // TestRealWorld tests a real-world case where:
    58  //   - There is a Platform Python project with an initial commit.
    59  //   - There is a local project that just checks it out.
    60  //   - The Platform project adds requests@2.30.0 (an older version).
    61  //   - The local project adds requests (latest version).
    62  //   - The local project pulls from the Platform project, resulting in conflicting times and version
    63  //     requirements for requests.
    64  func TestRealWorld(t *testing.T) {
    65  	script1, err := buildscript.New(fileutils.ReadFileUnsafe(filepath.Join("testdata", "buildscript1.as")))
    66  	require.NoError(t, err)
    67  	script2, err := buildscript.New(fileutils.ReadFileUnsafe(filepath.Join("testdata", "buildscript2.as")))
    68  	require.NoError(t, err)
    69  	result, err := generateDiff(script1, script2)
    70  	require.NoError(t, err)
    71  	assert.Equal(t, `<<<<<<< local
    72  at_time = "2023-10-16T22:20:29.000Z"
    73  =======
    74  at_time = "2023-08-01T16:20:11.985Z"
    75  >>>>>>> remote
    76  runtime = state_tool_artifacts_v1(
    77  	build_flags = [
    78  	],
    79  	camel_flags = [
    80  	],
    81  	src = sources
    82  )
    83  sources = solve(
    84  	at_time = at_time,
    85  	platforms = [
    86  		"78977bc8-0f32-519d-80f3-9043f059398c",
    87  		"7c998ec2-7491-4e75-be4d-8885800ef5f2",
    88  		"96b7e6f2-bebf-564c-bc1c-f04482398f38"
    89  	],
    90  	requirements = [
    91  		Req(name = "python", namespace = "language", version = Eq(value = "3.10.11")),
    92  <<<<<<< local
    93  		Req(name = "requests", namespace = "language/python")
    94  =======
    95  		Req(name = "requests", namespace = "language/python", version = Eq(value = "2.30.0"))
    96  >>>>>>> remote
    97  	],
    98  	solver_version = null
    99  )
   100  
   101  main = runtime`, result)
   102  }