vagrant/internal/core/testing_project.go

81 lines
1.8 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2021-04-06 18:03:30 -04:00
package core
2021-07-16 13:31:35 -04:00
import (
2022-10-05 20:21:00 -04:00
"io/ioutil"
"os"
"path/filepath"
2021-07-16 13:31:35 -04:00
2022-10-05 20:21:00 -04:00
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
2021-08-11 15:56:32 -04:00
"github.com/hashicorp/vagrant/internal/plugin"
2022-10-05 20:21:00 -04:00
"github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/require"
2021-07-16 13:31:35 -04:00
)
// TestProject returns a fully in-memory and side-effect free Project that
// can be used for testing. Additional options can be given to provide your own
// factories, configuration, etc.
func TestProject(t testing.T, opts ...BasisOption) *Project {
2022-10-05 20:21:00 -04:00
path := testTempDir(t)
name := filepath.Base(path)
2022-02-23 13:11:32 -05:00
b := TestBasis(t, opts...)
2022-07-07 14:04:26 -04:00
p, err := b.factory.NewProject(
[]ProjectOption{
WithBasis(b),
2022-10-05 20:21:00 -04:00
WithProjectRef(
&vagrant_plugin_sdk.Ref_Project{
Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
Name: name,
Path: path,
},
),
2022-07-07 14:04:26 -04:00
}...,
)
2022-10-05 20:21:00 -04:00
require.NoError(t, err)
require.NoError(t, p.Save())
2022-02-23 13:11:32 -05:00
return p
}
// TestMinimalProject uses a minimal basis to setup the most basic project
// that will work for testing
func TestMinimalProject(t testing.T) *Project {
2022-10-05 20:21:00 -04:00
path := testTempDir(t)
name := filepath.Base(path)
2022-02-23 13:30:15 -05:00
pluginManager := plugin.TestManager(t)
2022-02-23 13:11:32 -05:00
b := TestBasis(t, WithPluginManager(pluginManager))
2022-07-07 14:04:26 -04:00
p, err := b.factory.NewProject(
[]ProjectOption{
WithBasis(b),
2022-10-05 20:21:00 -04:00
WithProjectRef(
&vagrant_plugin_sdk.Ref_Project{
Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
Name: name,
Path: path,
},
),
2022-07-07 14:04:26 -04:00
}...,
)
require.NotEmpty(t, p.project.Path)
2022-10-05 20:21:00 -04:00
require.NoError(t, err)
require.NoError(t, p.Save())
2021-07-19 15:47:06 -04:00
return p
}
2022-10-05 20:21:00 -04:00
func testTempDir(t testing.T) string {
t.Helper()
dir, err := ioutil.TempDir("", "vagrant-test")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dir) })
return dir
}