Files
pack/core/generate/context_test.go
T

107 lines
2.5 KiB
Go

package generate
import (
"encoding/json"
"testing"
"github.com/gkampitakis/go-snaps/snaps"
"github.com/railwayapp/railpack/core/app"
"github.com/railwayapp/railpack/core/config"
"github.com/railwayapp/railpack/core/logger"
"github.com/railwayapp/railpack/core/plan"
"github.com/stretchr/testify/require"
)
type TestProvider struct{}
func (p *TestProvider) Plan(ctx *GenerateContext) error {
// mise
mise := ctx.GetMiseStepBuilder()
nodeRef := mise.Default("node", "18")
mise.Version(nodeRef, "18", "test")
// apt
aptStep := ctx.NewAptStepBuilder("test")
aptStep.AddInput(plan.NewStepInput(mise.Name()))
aptStep.AddAptPackage("git")
aptStep.AddAptPackage("neofetch")
// commands
installStep := ctx.NewCommandStep("install")
installStep.AddCommand(plan.NewExecCommand("npm install", plan.ExecOptions{}))
installStep.AddInput(plan.NewStepInput(aptStep.Name()))
installStep.Secrets = []string{}
buildStep := ctx.NewCommandStep("build")
buildStep.AddCommand(plan.NewExecCommand("npm run build", plan.ExecOptions{}))
buildStep.AddInput(plan.NewStepInput(installStep.Name()))
ctx.Deploy.Inputs = []plan.Input{
plan.NewStepInput(buildStep.Name()),
}
return nil
}
func CreateTestContext(t *testing.T, path string) *GenerateContext {
t.Helper()
userApp, err := app.NewApp(path)
require.NoError(t, err)
env := app.NewEnvironment(nil)
config := config.EmptyConfig()
ctx, err := NewGenerateContext(userApp, env, config, logger.NewLogger())
require.NoError(t, err)
return ctx
}
func TestGenerateContext(t *testing.T) {
ctx := CreateTestContext(t, "../../examples/node-npm")
provider := &TestProvider{}
require.NoError(t, provider.Plan(ctx))
// User defined config
configJSON := `{
"packages": {
"node": "20.18.2",
"go": "1.23.5",
"python": "3.13.1"
},
"aptPackages": ["curl"],
"steps": {
"build": {
"commands": ["echo building"]
}
},
"secrets": ["RAILWAY_SECRET_1", "RAILWAY_SECRET_2"],
"deploy": {
"startCommand": "echo hello",
"variables": {
"HELLO": "world"
}
}
}`
var config config.Config
require.NoError(t, json.Unmarshal([]byte(configJSON), &config))
ctx.Config = &config
buildPlan, _, err := ctx.Generate()
require.NoError(t, err)
buildPlanJSON, err := json.MarshalIndent(buildPlan, "", " ")
require.NoError(t, err)
var actualPlan map[string]interface{}
require.NoError(t, json.Unmarshal(buildPlanJSON, &actualPlan))
serializedPlan, err := json.MarshalIndent(actualPlan, "", " ")
require.NoError(t, err)
snaps.MatchJSON(t, serializedPlan)
}