Files
pack/core/core_test.go
T
0f95654ac5 fix: add dockerignore usage metadata (#351)
* feat: add dockerIgnore metadata field when .dockerignore exists

When a .dockerignore file is present and successfully parsed, the build
result metadata now includes "dockerIgnore": "true". This allows
consumers of the build plan to know whether dockerignore filtering was
applied during the build.

Changes:
- Set dockerIgnore metadata field in GenerateContext when .dockerignore exists
- Add tests to verify metadata is set correctly in both context and BuildResult
- Fix pre-existing test that fails when running as root

* refactor: move dockerIgnore metadata setting to dockerignore parsing logic

Move the responsibility of setting the dockerIgnore metadata from the
GenerateContext creation to the dockerignore parsing implementation.
This creates better separation of concerns by keeping dockerignore-
related logic in one place.

Changes:
- Add MetadataSetter interface to core/plan/dockerignore.go
- Update ParseWithLogging to accept metadata parameter and set metadata when dockerignore exists
- Update GenerateContext to pass metadata to ParseWithLogging
- Update tests to verify metadata is set during parsing
- Remove separate metadata setting logic from context creation

* refactor: simplify dockerignore metadata logic

Simplify the implementation to just check the parse results and set
metadata based on that. This removes the MetadataSetter interface and
keeps the logic straightforward - just 2 lines of code after parsing.

Also fix pre-existing tests that fail when running as root by adding
skip logic for permission-based tests.

Changes:
- Remove MetadataSetter interface from dockerignore.go
- Set dockerIgnore metadata after parsing based on parse results
- Add skip logic for permission tests that don't work as root

* refactor: remove unrelated skip logic from tests

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-01-15 10:25:42 -07:00

108 lines
3.0 KiB
Go

package core
import (
"os"
"path/filepath"
"testing"
"github.com/gkampitakis/go-snaps/snaps"
"github.com/railwayapp/railpack/core/app"
"github.com/railwayapp/railpack/core/logger"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
v := m.Run()
snaps.Clean(m, snaps.CleanOpts{Sort: true})
os.Exit(v)
}
// generate snapshot plan JSON for each build example and assert against it
func TestGenerateBuildPlanForExamples(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
// Get all the examples
examplesDir := filepath.Join(filepath.Dir(wd), "examples")
entries, err := os.ReadDir(examplesDir)
require.NoError(t, err)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
// For each example, generate a build plan that we can snapshot test
t.Run(entry.Name(), func(t *testing.T) {
examplePath := filepath.Join(examplesDir, entry.Name())
userApp, err := app.NewApp(examplePath)
require.NoError(t, err)
env := app.NewEnvironment(nil)
buildResult := GenerateBuildPlan(userApp, env, &GenerateBuildPlanOptions{})
if !buildResult.Success {
t.Fatalf("failed to generate build plan for %s: %s", entry.Name(), buildResult.Logs)
}
plan := buildResult.Plan
// Remove the mise.toml asset since the versions may change between runs
for _, step := range plan.Steps {
for name := range step.Assets {
if name == "mise.toml" {
step.Assets[name] = "[mise.toml]"
}
}
}
snaps.MatchStandaloneJSON(t, plan)
})
}
}
func TestGenerateConfigFromFile_NotFound(t *testing.T) {
// Use an existing example app directory so relative paths resolve
appPath := "../examples/config-file"
userApp, err := app.NewApp(appPath)
require.NoError(t, err)
env := app.NewEnvironment(nil)
l := logger.NewLogger()
options := &GenerateBuildPlanOptions{ConfigFilePath: "does-not-exist.railpack.json"}
cfg, genErr := GenerateConfigFromFile(userApp, env, options, l)
require.Error(t, genErr, "expected an error when explicit config file does not exist")
require.Nil(t, cfg, "config should be nil on error")
}
func TestGenerateConfigFromFile_Malformed(t *testing.T) {
appPath := "../examples/config-file"
userApp, err := app.NewApp(appPath)
require.NoError(t, err)
env := app.NewEnvironment(nil)
l := logger.NewLogger()
options := &GenerateBuildPlanOptions{ConfigFilePath: "railpack.malformed.json"}
cfg, genErr := GenerateConfigFromFile(userApp, env, options, l)
require.Error(t, genErr, "expected an error for malformed JSON config file")
require.Nil(t, cfg, "config should be nil on error")
}
func TestGenerateBuildPlan_DockerignoreMetadata(t *testing.T) {
appPath := "../examples/dockerignore"
userApp, err := app.NewApp(appPath)
require.NoError(t, err)
env := app.NewEnvironment(nil)
buildResult := GenerateBuildPlan(userApp, env, &GenerateBuildPlanOptions{})
require.True(t, buildResult.Success)
require.NotNil(t, buildResult.Metadata)
require.Equal(t, "true", buildResult.Metadata["dockerIgnore"])
}