| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| // Package testsetup provides a global init() shim to ensure glog is isolated |
| // in test environments. This package should be imported (potentially as a |
| // blank import) by every test to prevent collisions on /tmp when running |
| // parallel tests (e.g. --runs_per_test). |
| package testsetup |
| |
| import ( |
| "flag" |
| "os" |
| ) |
| |
| func init() { |
| // If running under Bazel's test runner and no log directory has been |
| // explicitly set, default glog output and Go's temp directory to the |
| // private test temporary directory. This ensures that parallel test runs |
| // of the same target do not collide when trying to create log files |
| // or symlinks in /tmp. |
| if testTmp := os.Getenv("TEST_TMPDIR"); testTmp != "" { |
| // Fix Go's standard temp directory for the process. |
| os.Setenv("TMPDIR", testTmp) |
| |
| // Explicitly tell glog to use it, preventing /tmp collisions for |
| // libraries that don't respect TMPDIR. |
| if f := flag.Lookup("log_dir"); f != nil && f.Value.String() == "" { |
| f.Value.Set(testTmp) |
| } |
| } |
| } |