1*9bb1b549SSpandan Das// Copyright 2020 The Bazel Authors. All rights reserved. 2*9bb1b549SSpandan Das// 3*9bb1b549SSpandan Das// Licensed under the Apache License, Version 2.0 (the "License"); 4*9bb1b549SSpandan Das// you may not use this file except in compliance with the License. 5*9bb1b549SSpandan Das// You may obtain a copy of the License at 6*9bb1b549SSpandan Das// 7*9bb1b549SSpandan Das// http://www.apache.org/licenses/LICENSE-2.0 8*9bb1b549SSpandan Das// 9*9bb1b549SSpandan Das// Unless required by applicable law or agreed to in writing, software 10*9bb1b549SSpandan Das// distributed under the License is distributed on an "AS IS" BASIS, 11*9bb1b549SSpandan Das// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9bb1b549SSpandan Das// See the License for the specific language governing permissions and 13*9bb1b549SSpandan Das// limitations under the License. 14*9bb1b549SSpandan Das 15*9bb1b549SSpandan Daspackage bzltestutil 16*9bb1b549SSpandan Das 17*9bb1b549SSpandan Das// This package must have no deps beyond Go SDK. 18*9bb1b549SSpandan Dasimport ( 19*9bb1b549SSpandan Das "fmt" 20*9bb1b549SSpandan Das "os" 21*9bb1b549SSpandan Das "path/filepath" 22*9bb1b549SSpandan Das "runtime" 23*9bb1b549SSpandan Das) 24*9bb1b549SSpandan Das 25*9bb1b549SSpandan Dasvar ( 26*9bb1b549SSpandan Das // Initialized by linker. 27*9bb1b549SSpandan Das RunDir string 28*9bb1b549SSpandan Das 29*9bb1b549SSpandan Das // Initial working directory. 30*9bb1b549SSpandan Das testExecDir string 31*9bb1b549SSpandan Das) 32*9bb1b549SSpandan Das 33*9bb1b549SSpandan Das// This initializer runs before any user packages. 34*9bb1b549SSpandan Dasfunc init() { 35*9bb1b549SSpandan Das var err error 36*9bb1b549SSpandan Das testExecDir, err = os.Getwd() 37*9bb1b549SSpandan Das if err != nil { 38*9bb1b549SSpandan Das panic(err) 39*9bb1b549SSpandan Das } 40*9bb1b549SSpandan Das 41*9bb1b549SSpandan Das // Check if we're being run by Bazel and change directories if so. 42*9bb1b549SSpandan Das // TEST_SRCDIR and TEST_WORKSPACE are set by the Bazel test runner, so that makes a decent proxy. 43*9bb1b549SSpandan Das testSrcDir, hasSrcDir := os.LookupEnv("TEST_SRCDIR") 44*9bb1b549SSpandan Das testWorkspace, hasWorkspace := os.LookupEnv("TEST_WORKSPACE") 45*9bb1b549SSpandan Das if hasSrcDir && hasWorkspace && RunDir != "" { 46*9bb1b549SSpandan Das abs := RunDir 47*9bb1b549SSpandan Das if !filepath.IsAbs(RunDir) { 48*9bb1b549SSpandan Das abs = filepath.Join(testSrcDir, testWorkspace, RunDir) 49*9bb1b549SSpandan Das } 50*9bb1b549SSpandan Das err := os.Chdir(abs) 51*9bb1b549SSpandan Das // Ignore the Chdir err when on Windows, since it might have have runfiles symlinks. 52*9bb1b549SSpandan Das // https://github.com/bazelbuild/rules_go/pull/1721#issuecomment-422145904 53*9bb1b549SSpandan Das if err != nil && runtime.GOOS != "windows" { 54*9bb1b549SSpandan Das panic(fmt.Sprintf("could not change to test directory: %v", err)) 55*9bb1b549SSpandan Das } 56*9bb1b549SSpandan Das if err == nil { 57*9bb1b549SSpandan Das os.Setenv("PWD", abs) 58*9bb1b549SSpandan Das } 59*9bb1b549SSpandan Das } 60*9bb1b549SSpandan Das} 61