xref: /aosp_15_r20/external/bazelbuild-rules_android/src/common/golang/runfilelocation_test.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2023 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package runfilelocation
16
17import (
18	"io/ioutil"
19	"os"
20	"testing"
21)
22
23func TestValidRunfileLocation(t *testing.T) {
24	// Check that Find() returns a valid path to a runfile
25	runfilePath := "src/common/golang/a.txt"
26
27	absRunFilePath, err := Find(runfilePath)
28	if err != nil {
29		t.Errorf("Runfile path through Runfilelocation() failed: %v", err)
30	}
31
32	// Check that the path actually exists
33	contents, err := ioutil.ReadFile(absRunFilePath)
34	text := string(contents)
35	if err != nil {
36		t.Errorf("Could not read file: %v", err)
37	}
38
39	if text != "hello world\n" {
40		t.Errorf("Expected 'hello world' in file, got %v instead.", text)
41	}
42}
43
44func TestInvalidRunfileLocation(t *testing.T) {
45	invalidRunfilePath := "src/common/golang/b.txt"
46
47	runfileLocationShouldNotExist, err := Find(invalidRunfilePath)
48	if err != nil {
49		// Even if the path is invalid, runfilelocation.Find() should return the path to where it _thinks_
50		// the runfile should exist.
51		t.Errorf("Unexpected error: %v should have returned a runfile path. Instead got %v", invalidRunfilePath, err)
52	}
53
54	// Check that the invalid runfile path actually does not exist.
55	if _, err := os.Stat(runfileLocationShouldNotExist); err == nil {
56		t.Errorf("Expected error, file should not have been found: %v", runfileLocationShouldNotExist)
57	}
58}
59