Skip to content

Commit

Permalink
fix: clear the deploy directory before building, fixes #1495 #1451 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
safeer authored May 16, 2024
1 parent 107cf29 commit db6003f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions buildengine/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func buildModule(ctx context.Context, sch *schema.Schema, module Module, filesTr
logger := log.FromContext(ctx).Scope(module.Module)
ctx = log.ContextWithLogger(ctx, logger)

// clear stale module errors before extracting schema
if err := os.RemoveAll(filepath.Join(module.AbsDeployDir(), module.Errors)); err != nil {
// clear the deploy directory before extracting schema
if err := os.RemoveAll(module.AbsDeployDir()); err != nil {
return fmt.Errorf("failed to clear errors: %w", err)
}

Expand Down
15 changes: 15 additions & 0 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ func Nothing(context.Context) error {
})
}

func TestGoBuildClearsBuildDir(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "test"},
},
}
bctx := buildContext{
moduleDir: "testdata/projects/another",
buildDir: "_ftl",
sch: sch,
}
testBuildClearsBuildDir(t, bctx)
}

func TestMetadataImportsExcluded(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
Expand Down
15 changes: 15 additions & 0 deletions buildengine/build_kotlin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ package ftl.test
})
}

func TestKotlinBuildClearsBuildDir(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "test"},
},
}
bctx := buildContext{
moduleDir: "testdata/projects/echokotlin",
buildDir: "target",
sch: sch,
}
testBuildClearsBuildDir(t, bctx)
}

func TestGenerateAllTypes(t *testing.T) {
if testing.Short() {
t.SkipNow()
Expand Down
29 changes: 29 additions & 0 deletions buildengine/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ func testBuild(
assert.NoError(t, err, "Error removing build directory")
}

func testBuildClearsBuildDir(t *testing.T, bctx buildContext) {
t.Helper()
ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{}))
abs, err := filepath.Abs(bctx.moduleDir)
assert.NoError(t, err, "Error getting absolute path for module directory")

// build to generate the build directory
module, err := LoadModule(abs)
assert.NoError(t, err)
err = Build(ctx, bctx.sch, module, &mockModifyFilesTransaction{})
assert.NoError(t, err)

// create a temporary file in the build directory
buildDir := filepath.Join(bctx.moduleDir, bctx.buildDir)
tempFile, err := os.Create(filepath.Join(buildDir, "test-clear-build.tmp"))
assert.NoError(t, err, "Error creating temporary file in module directory")
tempFile.Close()

// build to clear the old build directory
module, err = LoadModule(abs)
assert.NoError(t, err)
err = Build(ctx, bctx.sch, module, &mockModifyFilesTransaction{})
assert.NoError(t, err)

// ensure the temporary file was removed
_, err = os.Stat(filepath.Join(buildDir, "test-clear-build.tmp"))
assert.Error(t, err, "Build directory was not removed")
}

func assertGeneratedModule(generatedModulePath string, expectedContent string) assertion {
return func(t testing.TB, bctx buildContext) error {
t.Helper()
Expand Down

0 comments on commit db6003f

Please sign in to comment.