summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/testutil_test.go
blob: 68f79187d3aed87453dce751279ad33905cf2e03 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package cmd

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"

	"github.com/spf13/viper"
	"github.com/vespa-engine/vespa/client/go/mock"
)

func newTestCLI(t *testing.T, envVars ...string) (*CLI, *bytes.Buffer, *bytes.Buffer) {
	t.Cleanup(viper.Reset)
	homeDir := filepath.Join(t.TempDir(), ".vespa")
	cacheDir := filepath.Join(t.TempDir(), ".cache", "vespa")
	env := []string{"VESPA_CLI_HOME=" + homeDir, "VESPA_CLI_CACHE_DIR=" + cacheDir}
	env = append(env, envVars...)
	var (
		stdout bytes.Buffer
		stderr bytes.Buffer
	)
	cli, err := New(&stdout, &stderr, env)
	if err != nil {
		t.Fatal(err)
	}
	cli.httpClient = &mock.HTTPClient{}
	cli.exec = &mock.Exec{}
	return cli, &stdout, &stderr
}

func mockApplicationPackage(t *testing.T, java bool) string {
	dir := t.TempDir()
	appDir := filepath.Join(dir, "src", "main", "application")
	if err := os.MkdirAll(appDir, 0755); err != nil {
		t.Fatal(err)
	}
	servicesXML := filepath.Join(appDir, "services.xml")
	if _, err := os.Create(servicesXML); err != nil {
		t.Fatal(err)
	}
	if java {
		if _, err := os.Create(filepath.Join(dir, "pom.xml")); err != nil {
			t.Fatal(err)
		}
	}
	return dir
}