1 /* Copyright 2016 The TensorFlow 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 ==============================================================================*/
15 
16 #include "tensorflow/core/platform/test.h"
17 
18 #include <cstdlib>
19 
20 #include "tensorflow/core/platform/logging.h"
21 #include "tensorflow/core/platform/net.h"
22 #include "tensorflow/core/platform/path.h"
23 #include "tensorflow/core/platform/strcat.h"
24 #include "tensorflow/core/platform/types.h"
25 
26 namespace tensorflow {
27 namespace testing {
28 
TmpDir()29 string TmpDir() {
30   // 'bazel test' sets TEST_TMPDIR
31   const char* env = getenv("TEST_TMPDIR");
32   if (env && env[0] != '\0') {
33     return env;
34   }
35   env = getenv("TMPDIR");
36   if (env && env[0] != '\0') {
37     return env;
38   }
39   return "/tmp";
40 }
41 
SrcDir()42 string SrcDir() {
43   // Bazel makes data dependencies available via a relative path.
44   return "";
45 }
46 
RandomSeed()47 int RandomSeed() {
48   const char* env = getenv("TEST_RANDOM_SEED");
49   int result;
50   if (env && sscanf(env, "%d", &result) == 1) {
51     return result;
52   }
53   return 301;
54 }
55 
PickUnusedPortOrDie()56 int PickUnusedPortOrDie() { return internal::PickUnusedPortOrDie(); }
57 
TensorFlowSrcRoot()58 string TensorFlowSrcRoot() {
59   // 'bazel test' sets TEST_SRCDIR, and also TEST_WORKSPACE if a new
60   // enough version of bazel is used.
61   const char* env = getenv("TEST_SRCDIR");
62   const char* workspace = getenv("TEST_WORKSPACE");
63   if (env && env[0] != '\0') {
64     if (workspace && workspace[0] != '\0') {
65       return io::JoinPath(env, workspace, "tensorflow");
66     }
67     return io::JoinPath(env, "tensorflow");
68   }
69   LOG(WARNING) << "TEST_SRCDIR environment variable not set: "
70                << "using $PWD/tensorflow as TensorFlowSrcRoot() for tests.";
71   return "tensorflow";
72 }
73 
74 }  // namespace testing
75 }  // namespace tensorflow
76