Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
}
logger.Debugf("Use build directory: %s", buildDir)

targets, err := docs.UpdateReadmes(packageRoot)
targets, err := docs.UpdateReadmes(packageRoot, "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could buildDir be used here as that folder has been created above?

Suggested change
targets, err := docs.UpdateReadmes(packageRoot, "")
targets, err := docs.UpdateReadmes(packageRoot, buildDir)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, seeing that actually maybe we don't need all the magic below to create the build dir, as is being created explicitly here. Maybe I can remove some more code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to make the build dir always explicit, but there are many things that end up using a builder and would need to be refactored to receive also a build dir. For example the installers, and resources.FleetPackage.

Though I have also found that the value returned by BuildPackagesDirectory is expected to have the packages part, so I have fixed that, and then we can use buildDir here.

Changed in ead911b.

if err != nil {
return fmt.Errorf("updating files failed: %w", err)
}
Expand Down
14 changes: 9 additions & 5 deletions internal/builder/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var repositoryLicenseEnv = environment.WithElasticPackagePrefix("REPOSITORY_LICE

type BuildOptions struct {
PackageRoot string
BuildDir string

CreateZip bool
SignPackage bool
Expand Down Expand Up @@ -73,10 +74,13 @@ func findBuildDirectory() (string, bool, error) {
}

// BuildPackagesDirectory function locates the target build directory for the package.
func BuildPackagesDirectory(packageRoot string) (string, error) {
buildDir, err := buildPackagesRootDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build packages root directory: %w", err)
func BuildPackagesDirectory(packageRoot string, buildDir string) (string, error) {
if buildDir == "" {
d, err := buildPackagesRootDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build packages root directory: %w", err)
}
buildDir = d
}
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
if err != nil {
Expand Down Expand Up @@ -144,7 +148,7 @@ func FindBuildPackagesDirectory() (string, bool, error) {

// BuildPackage function builds the package.
func BuildPackage(ctx context.Context, options BuildOptions) (string, error) {
destinationDir, err := BuildPackagesDirectory(options.PackageRoot)
destinationDir, err := BuildPackagesDirectory(options.PackageRoot, options.BuildDir)
if err != nil {
return "", fmt.Errorf("can't locate build directory: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/docs/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func isReadmeUpToDate(fileName, packageRoot string) (bool, string, error) {

// UpdateReadmes function updates all .md readme files using a defined template
// files. The function doesn't perform any action if the template file is not present.
func UpdateReadmes(packageRoot string) ([]string, error) {
func UpdateReadmes(packageRoot, buildDir string) ([]string, error) {
readmeFiles, err := filepath.Glob(filepath.Join(packageRoot, "_dev", "build", "docs", "*.md"))
if err != nil {
return nil, fmt.Errorf("reading directory entries failed: %w", err)
Expand All @@ -103,7 +103,7 @@ func UpdateReadmes(packageRoot string) ([]string, error) {
var targets []string
for _, filePath := range readmeFiles {
fileName := filepath.Base(filePath)
target, err := updateReadme(fileName, packageRoot)
target, err := updateReadme(fileName, packageRoot, buildDir)
if err != nil {
return nil, fmt.Errorf("updating readme file %s failed: %w", fileName, err)
}
Expand All @@ -115,7 +115,7 @@ func UpdateReadmes(packageRoot string) ([]string, error) {
return targets, nil
}

func updateReadme(fileName, packageRoot string) (string, error) {
func updateReadme(fileName, packageRoot, buildDir string) (string, error) {
logger.Debugf("Update the %s file", fileName)

rendered, shouldBeRendered, err := generateReadme(fileName, packageRoot)
Expand All @@ -131,7 +131,7 @@ func updateReadme(fileName, packageRoot string) (string, error) {
return "", fmt.Errorf("writing %s file failed: %w", fileName, err)
}

packageBuildRoot, err := builder.BuildPackagesDirectory(packageRoot)
packageBuildRoot, err := builder.BuildPackagesDirectory(packageRoot, buildDir)
if err != nil {
return "", fmt.Errorf("package build root not found: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/packages/archetype/data_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ func createAndCheckDataStream(t *testing.T, pd PackageDescriptor, dd DataStreamD
wd, err := os.Getwd()
require.NoError(t, err)

tempDir, err := os.MkdirTemp("", "archetype-create-data-stream-")
tempDir := makeInRepoBuildTempDir(t)
require.NoError(t, err)

os.Chdir(tempDir)
defer func() {
os.Chdir(wd)
os.RemoveAll(tempDir)
}()

err = CreatePackage(pd)
Expand Down
42 changes: 39 additions & 3 deletions internal/packages/archetype/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
package archetype

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-package/internal/builder"
"github.com/elastic/elastic-package/internal/docs"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/validation"
)

func TestPackage(t *testing.T) {
Expand All @@ -36,7 +39,7 @@ func TestPackage(t *testing.T) {
}

func createAndCheckPackage(t *testing.T, pd PackageDescriptor, valid bool) {
tempDir := t.TempDir()
tempDir := makeInRepoBuildTempDir(t)
err := createPackageInDir(pd, tempDir)
require.NoError(t, err)

Expand Down Expand Up @@ -90,8 +93,22 @@ func createPackageDescriptorForTest(packageType, kibanaVersion string) PackageDe
}
}

func buildPackage(t *testing.T, packageRoot string) error {
buildDir := makeInRepoBuildTempDir(t)
_, err := docs.UpdateReadmes(packageRoot, buildDir)
if err != nil {
return err
}

_, err = builder.BuildPackage(context.Background(), builder.BuildOptions{
PackageRoot: packageRoot,
BuildDir: buildDir,
})
return err
}

func checkPackage(t *testing.T, packageRoot string, valid bool) {
err, _ := validation.ValidateAndFilterFromPath(packageRoot)
err := buildPackage(t, packageRoot)
if !valid {
assert.Error(t, err)
return
Expand Down Expand Up @@ -124,3 +141,22 @@ func checkPackage(t *testing.T, packageRoot string, valid bool) {
})
}
}

// makeInRepoBuildTempDir mimicks t.TempDir(), but creates the directory inside the current
// directory.
// FIXME: It should be possible to use t.TempDir(), but conflicts with links resolution, as
// t.TempDir() creates the directory out of the repository. We should refactor links resolution
// so it can write files out of the repository.
// https://github.com/elastic/elastic-package/issues/2797
func makeInRepoBuildTempDir(t *testing.T) string {
t.Helper()
cwd, err := os.Getwd()
require.NoError(t, err)
dir, err := os.MkdirTemp(cwd, "_build-test-*")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(dir)
assert.NoError(t, err)
})
return dir
}