2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-11-30 13:40:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-11-18 09:57:31 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadNotExists(t *testing.T) {
|
|
|
|
b, err := Load("/doesntexist")
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
assert.Nil(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadExists(t *testing.T) {
|
2022-11-28 09:59:43 +00:00
|
|
|
b, err := Load("./tests/basic")
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, "basic", b.Config.Bundle.Name)
|
|
|
|
}
|
2022-11-30 13:40:41 +00:00
|
|
|
|
|
|
|
func TestBundleCacheDir(t *testing.T) {
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "bundle.yml"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
f1.Close()
|
|
|
|
|
|
|
|
bundle, err := Load(projectDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Artificially set environment.
|
|
|
|
// This is otherwise done by [mutators.SelectEnvironment].
|
|
|
|
bundle.Config.Bundle.Environment = "default"
|
|
|
|
|
|
|
|
cacheDir, err := bundle.CacheDir()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, strings.HasPrefix(cacheDir, projectDir))
|
|
|
|
}
|
2023-01-27 15:57:39 +00:00
|
|
|
|
|
|
|
func TestBundleMustLoadSuccess(t *testing.T) {
|
|
|
|
t.Setenv(envBundleRoot, "./tests/basic")
|
|
|
|
b, err := MustLoad()
|
|
|
|
require.NoError(t, err)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.Equal(t, "tests/basic", filepath.ToSlash(b.Config.Path))
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleMustLoadFailureWithEnv(t *testing.T) {
|
|
|
|
t.Setenv(envBundleRoot, "./tests/doesntexist")
|
|
|
|
_, err := MustLoad()
|
|
|
|
require.Error(t, err, "not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleMustLoadFailureIfNotFound(t *testing.T) {
|
|
|
|
chdir(t, t.TempDir())
|
|
|
|
_, err := MustLoad()
|
|
|
|
require.Error(t, err, "unable to find bundle root")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadSuccess(t *testing.T) {
|
|
|
|
t.Setenv(envBundleRoot, "./tests/basic")
|
|
|
|
b, err := TryLoad()
|
|
|
|
require.NoError(t, err)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.Equal(t, "tests/basic", filepath.ToSlash(b.Config.Path))
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadFailureWithEnv(t *testing.T) {
|
|
|
|
t.Setenv(envBundleRoot, "./tests/doesntexist")
|
|
|
|
_, err := TryLoad()
|
|
|
|
require.Error(t, err, "not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadOkIfNotFound(t *testing.T) {
|
|
|
|
chdir(t, t.TempDir())
|
|
|
|
b, err := TryLoad()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, b)
|
|
|
|
}
|