xref: /aosp_15_r20/external/bazelbuild-rules_go/tests/bcr/runfiles/runfiles_test.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1package runfiles
2
3import (
4	"os"
5	"testing"
6
7	"github.com/bazelbuild/rules_go/go/runfiles"
8)
9
10func TestRunfilesApparent(t *testing.T) {
11	path, err := runfiles.Rlocation("other_module/bar.txt")
12	if err != nil {
13		t.Fatalf("runfiles.Path: %v", err)
14	}
15	assertRunfile(t, path)
16}
17
18func TestRunfilesApparentSourceRepositoryOption(t *testing.T) {
19	r, err := runfiles.New(runfiles.SourceRepo(runfiles.CurrentRepository()))
20	if err != nil {
21		t.Fatalf("runfiles.New: %v", err)
22	}
23	path, err := r.Rlocation("other_module/bar.txt")
24	if err != nil {
25		t.Fatalf("runfiles.Path: %v", err)
26	}
27	assertRunfile(t, path)
28}
29
30func TestRunfilesApparentWithSourceRepository(t *testing.T) {
31	r, err := runfiles.New()
32	if err != nil {
33		t.Fatalf("runfiles.New: %v", err)
34	}
35	r = r.WithSourceRepo(runfiles.CurrentRepository())
36	path, err := r.Rlocation("other_module/bar.txt")
37	if err != nil {
38		t.Fatalf("runfiles.Path: %v", err)
39	}
40	assertRunfile(t, path)
41}
42
43func TestRunfilesFromApparent(t *testing.T) {
44	path, err := runfiles.RlocationFrom("other_module/bar.txt", runfiles.CurrentRepository())
45	if err != nil {
46		t.Fatalf("runfiles.Path: %v", err)
47	}
48	assertRunfile(t, path)
49}
50
51func TestRunfilesCanonical(t *testing.T) {
52	path, err := runfiles.Rlocation(os.Args[1])
53	if err != nil {
54		t.Fatalf("runfiles.Path: %v", err)
55	}
56	assertRunfile(t, path)
57}
58
59func assertRunfile(t *testing.T, path string) {
60	content, err := os.ReadFile(path)
61	if err != nil {
62		t.Fatalf("os.ReadFile: %v", err)
63	}
64	if string(content) != "hello\n" {
65		t.Fatalf("got %q; want %q", content, "hello\n")
66	}
67}
68