databricks-cli/internal/init_test.go

92 lines
3.2 KiB
Go
Raw Normal View History

2023-05-17 11:41:15 +00:00
package internal
import (
"fmt"
"os"
"path/filepath"
"testing"
_ "github.com/databricks/cli/cmd/bundle"
2023-05-17 12:07:57 +00:00
"github.com/databricks/cli/cmd/root"
2023-05-17 11:41:15 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func assertFileContains(t *testing.T, path string, substr string) {
b, err := os.ReadFile(path)
require.NoError(t, err)
assert.Contains(t, string(b), substr)
}
2023-05-17 11:43:27 +00:00
func TestTemplateInitializationForDevConfig(t *testing.T) {
2023-05-17 11:41:15 +00:00
// create target directory with the input config
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "config.json"))
require.NoError(t, err)
_, err = f.WriteString(`
{
"project_name": "development_project",
"cloud_type": "AWS",
"ci_type": "github",
"is_production": false
}
`)
2023-05-17 13:33:04 +00:00
f.Close()
2023-05-17 11:41:15 +00:00
require.NoError(t, err)
// materialize the template
cmd := root.RootCmd
2023-05-23 17:02:09 +00:00
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp, "--config-file", filepath.Join(tmp, "config.json")})
2023-05-17 11:41:15 +00:00
err = cmd.Execute()
require.NoError(t, err)
// assert on materialized template
assert.FileExists(t, filepath.Join(tmp, "development_project", "aws_file"))
assert.FileExists(t, filepath.Join(tmp, "development_project", ".github"))
assert.NoFileExists(t, filepath.Join(tmp, "development_project", "azure_file"))
assertFileContains(t, filepath.Join(tmp, "development_project", "aws_file"), "This file should only be generated for AWS")
assertFileContains(t, filepath.Join(tmp, "development_project", ".github"), "This is a development project")
}
2023-05-17 11:43:27 +00:00
func TestTemplateInitializationForProdConfig(t *testing.T) {
2023-05-17 11:41:15 +00:00
// create target directory with the input config
tmp := t.TempDir()
2023-05-22 03:58:05 +00:00
// create target directory to with the input config
configDir := filepath.Join(tmp, "dir-with-config")
err := os.Mkdir(configDir, os.ModePerm)
require.NoError(t, err)
f, err := os.Create(filepath.Join(configDir, "my_config.json"))
2023-05-17 11:41:15 +00:00
require.NoError(t, err)
_, err = f.WriteString(`
{
"project_name": "production_project",
"cloud_type": "Azure",
"ci_type": "azure_devops",
"is_production": true
}
`)
2023-05-17 13:33:04 +00:00
f.Close()
2023-05-17 11:41:15 +00:00
require.NoError(t, err)
2023-05-22 03:58:05 +00:00
// create directory to initialize the template instance within
instanceDir := filepath.Join(tmp, "dir-with-instance")
err = os.Mkdir(instanceDir, os.ModePerm)
require.NoError(t, err)
2023-05-17 11:41:15 +00:00
// materialize the template
cmd := root.RootCmd
childCommands := cmd.Commands()
fmt.Println(childCommands)
2023-05-22 03:58:05 +00:00
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", instanceDir, "--config-file", filepath.Join(configDir, "my_config.json")})
2023-05-17 11:41:15 +00:00
err = cmd.Execute()
require.NoError(t, err)
// assert on materialized template
2023-05-22 03:58:05 +00:00
assert.FileExists(t, filepath.Join(instanceDir, "production_project", "azure_file"))
assert.FileExists(t, filepath.Join(instanceDir, "production_project", ".azure_devops"))
assert.NoFileExists(t, filepath.Join(instanceDir, "production_project", "aws_file"))
assertFileContains(t, filepath.Join(instanceDir, "production_project", "azure_file"), "This file should only be generated for Azure")
assertFileContains(t, filepath.Join(instanceDir, "production_project", ".azure_devops"), "This is a production project")
2023-05-17 11:41:15 +00:00
}