File: testing_project.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (77 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package core

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
	"github.com/hashicorp/vagrant/internal/plugin"
	"github.com/mitchellh/go-testing-interface"
	"github.com/stretchr/testify/require"
)

// 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 {
	path := testTempDir(t)
	name := filepath.Base(path)

	b := TestBasis(t, opts...)
	p, err := b.factory.NewProject(
		[]ProjectOption{
			WithBasis(b),
			WithProjectRef(
				&vagrant_plugin_sdk.Ref_Project{
					Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
					Name:  name,
					Path:  path,
				},
			),
		}...,
	)

	require.NoError(t, err)
	require.NoError(t, p.Save())

	return p
}

// TestMinimalProject uses a minimal basis to setup the most basic project
// that will work for testing
func TestMinimalProject(t testing.T) *Project {
	path := testTempDir(t)
	name := filepath.Base(path)

	pluginManager := plugin.TestManager(t)
	b := TestBasis(t, WithPluginManager(pluginManager))
	p, err := b.factory.NewProject(
		[]ProjectOption{
			WithBasis(b),
			WithProjectRef(
				&vagrant_plugin_sdk.Ref_Project{
					Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
					Name:  name,
					Path:  path,
				},
			),
		}...,
	)

	require.NotEmpty(t, p.project.Path)

	require.NoError(t, err)
	require.NoError(t, p.Save())

	return p
}

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
}