diff --git a/internal/core/target_test.go b/internal/core/target_test.go index d0e27f4e8..52c4c9b44 100644 --- a/internal/core/target_test.go +++ b/internal/core/target_test.go @@ -3,20 +3,11 @@ package core import ( "testing" - "github.com/hashicorp/go-hclog" "github.com/hashicorp/vagrant-plugin-sdk/core" - "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" ) -func targetInstance() *Target { - return &Target{ - target: &vagrant_server.Target{}, - logger: hclog.New(&hclog.LoggerOptions{Name: "test"}), - } -} - func TestTargetSpecializeMachine(t *testing.T) { - tt := targetInstance() + tt, _ := TestTarget(t) specialized, err := tt.Specialize((*core.Machine)(nil)) if err != nil { @@ -29,7 +20,7 @@ func TestTargetSpecializeMachine(t *testing.T) { } func TestTargetSpecializeBad(t *testing.T) { - tt := targetInstance() + tt, _ := TestTarget(t) specialized, err := tt.Specialize((*core.Project)(nil)) if err != nil { @@ -43,9 +34,8 @@ func TestTargetSpecializeBad(t *testing.T) { func TestRun(t *testing.T) { // TODO: needs - // - to be able to create a Target with a project // - to be able to create a Task - // tt := targetInstance() + // tt, _ := TestTarget(t) // ctx := context.Background() // tk := &vagrant_server.Task{} diff --git a/internal/core/testing_project.go b/internal/core/testing_project.go index d588b2c31..024de8c8d 100644 --- a/internal/core/testing_project.go +++ b/internal/core/testing_project.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/vagrant-plugin-sdk/datadir" "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" "github.com/hashicorp/vagrant/internal/factory" + "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" "github.com/hashicorp/vagrant/internal/server/singleprocess" ) @@ -31,6 +32,34 @@ var TestingTypeMap = map[component.Type]interface{}{ component.SyncedFolderType: (*component.SyncedFolder)(nil), } +// TestTarget returns a fully in-memory and side-effect free Target that +// can be used for testing. Additional options can be given to provide your own +// factories, configuration, etc. +func TestTarget(t testing.T, opts ...BasisOption) (target *Target, err error) { + tp := TestProject(t, opts...) + // vagrantServerTarget, err := + tp.basis.client.UpsertTarget( + context.Background(), + &vagrant_server.UpsertTargetRequest{ + Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project), + Target: &vagrant_server.Target{ + Name: "test-target", + Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project), + }, + }, + ) + target, err = tp.LoadTarget([]TargetOption{ + WithTargetRef(&vagrant_plugin_sdk.Ref_Target{Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project), Name: "test-target"}), + }...) + + // return &Target{ + // target: &vagrant_server.Target{}, + // logger: hclog.New(&hclog.LoggerOptions{Name: "test"}), + // } + + return +} + // 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.