github.com/bazelbuild/rules_webtesting@v0.2.0/java/com/google/testing/bazel/Bazel.java (about) 1 // Copyright 2016 Google Inc. 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 // 15 // ////////////////////////////////////////////////////////////////////////////// 16 // 17 package com.google.testing.bazel; 18 19 import com.google.common.annotations.VisibleForTesting; 20 import com.google.common.base.Preconditions; 21 import java.io.IOException; 22 import java.nio.file.FileSystems; 23 import java.nio.file.Files; 24 import java.nio.file.Path; 25 import java.util.Optional; 26 27 /** Provides access to various Bazel environment variables. */ 28 public class Bazel { 29 30 private static Bazel instance; 31 32 private static final String TEST_SRCDIR = "TEST_SRCDIR"; 33 private static final String TEST_TMPDIR = "TEST_TMPDIR"; 34 private static final String TEST_WORKSPACE = "TEST_WORKSPACE"; 35 private static final String DEFAULT_WORKSPACE = "io_bazel_rules_webtesting"; 36 37 private final Path runfilesDir; 38 private final Optional<Path> testTmpDir; 39 private final String testWorkspace; 40 41 @VisibleForTesting 42 Bazel(Path runfilesDir, Optional<Path> testTmpDir, String testWorkspace) { 43 this.runfilesDir = Preconditions.checkNotNull(runfilesDir); 44 this.testTmpDir = Preconditions.checkNotNull(testTmpDir); 45 this.testWorkspace = Preconditions.checkNotNull(testWorkspace); 46 } 47 48 public static synchronized Bazel getInstance() { 49 if (instance == null) { 50 String tmpDirPath = System.getenv(TEST_TMPDIR); 51 Optional<Path> tmpDir = Optional.empty(); 52 if (tmpDirPath != null && !tmpDirPath.isEmpty()) { 53 tmpDir = Optional.of(FileSystems.getDefault().getPath(tmpDirPath)); 54 } 55 String testWorkspace = System.getenv(TEST_WORKSPACE); 56 if (testWorkspace == null || testWorkspace.isEmpty()) { 57 testWorkspace = DEFAULT_WORKSPACE; 58 } 59 instance = 60 new Bazel( 61 FileSystems.getDefault().getPath(System.getenv(TEST_SRCDIR)), tmpDir, testWorkspace); 62 } 63 return instance; 64 } 65 66 /** Returns the path of the runfile in the default workspace. */ 67 public Path runfile(String path) throws IOException { 68 Path candidate = runfilesDir.resolve(path); 69 if (candidate.toFile().exists()) { 70 return candidate; 71 } 72 candidate = runfilesDir.resolve(testWorkspace).resolve(path); 73 if (candidate.toFile().exists()) { 74 return candidate; 75 } 76 throw new IOException("Can not find runfile: " + path); 77 } 78 79 /** Returns a new temporary subdirectory in the test temporary directory. */ 80 public Path newTmpDir(String prefix) throws IOException { 81 if (testTmpDir.isPresent()) { 82 return Files.createTempDirectory(testTmpDir.get(), prefix); 83 } 84 return Files.createTempDirectory(prefix); 85 } 86 }