github.com/please-build/go-rules/tools/please_go@v0.0.0-20240319165128-ea27d6f5caba/install/install_test.go (about)

     1  // package main_test has some basic tests to check please_go_install works but it's quite hard to build up real world
     2  // examples here so the majority of coverage comes from //test/go_modules
     3  package install
     4  
     5  import (
     6  	"bytes"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/please-build/go-rules/tools/please_go/install/exec"
    15  )
    16  
    17  func TestMissingImport(t *testing.T) {
    18  	install, stdOut, _ := newInstall()
    19  	err := install.Install([]string{"missing_import"})
    20  	require.Error(t, err)
    21  	assert.Contains(t, []string{
    22  		filepath.Join(os.Getenv("TMP_DIR"), "tools/please_go/install/test_data/example.com/missing_import/missing_import.go:3:8: could not import github.com/doesnt-exist (open : no such file or directory)\n"),    // go 1.18
    23  		filepath.Join(os.Getenv("TMP_DIR"), "tools/please_go/install/test_data/example.com/missing_import/missing_import.go:3:16: could not import github.com/doesnt-exist (open : no such file or directory)\n"),   // go 1.18 (sometimes the column is different?)
    24  		filepath.Join(os.Getenv("TMP_DIR"), "tools/please_go/install/test_data/example.com/missing_import/missing_import.go:3:8: could not import \"github.com/doesnt-exist\": open : no such file or directory\n"), // go 1.17
    25  		filepath.Join(os.Getenv("TMP_DIR"), "tools/please_go/install/test_data/example.com/missing_import/missing_import.go:3:8: can't find import: \"github.com/doesnt-exist\"\n"),                                 // go 1.16
    26  	}, stdOut.String())
    27  }
    28  
    29  func TestNoSources(t *testing.T) {
    30  	install, _, _ := newInstall()
    31  
    32  	err := install.Install([]string{"no_sources"})
    33  	require.Error(t, err)
    34  	assert.Contains(t, err.Error(), "failed to compile example.com/no_sources: no buildable Go source files")
    35  }
    36  
    37  func TestLocalImports(t *testing.T) {
    38  	install, _, _ := newInstall()
    39  
    40  	err := install.Install([]string{"local_imports/foo"})
    41  	require.NoError(t, err)
    42  
    43  	expectedOut := "out/example.com/local_imports/foo/foo.a"
    44  	_, err = os.Lstat(expectedOut)
    45  	require.NoError(t, err, "output file %s wasn't created", expectedOut)
    46  }
    47  
    48  func newInstall() (*PleaseGoInstall, *bytes.Buffer, *bytes.Buffer) {
    49  	goTool := filepath.Join(os.Getenv("DATA_GO_TOOL"), "bin/go")
    50  	install := New([]string{}, "tools/please_go/install/test_data/example.com", "example.com", "tools/please_go/install/test_data/empty.importcfg", "", "", goTool, "cc", "pkg-config", "out", "")
    51  
    52  	stdOut := &bytes.Buffer{}
    53  	stdIn := &bytes.Buffer{}
    54  	install.tc.Exec = &exec.Executor{
    55  		Stdout: stdOut,
    56  		Stderr: stdIn,
    57  	}
    58  	return install, stdOut, stdIn
    59  }
    60  
    61  func TestMain(m *testing.M) {
    62  	f, err := os.Create("tools/please_go/install/test_data/empty.importcfg")
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	f.Close()
    67  
    68  	os.Exit(m.Run())
    69  }