Home | History | Annotate | Download | only in tests
      1 /* Copyright 2017 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/compiler/xla/tests/test_macros.h"
     17 
     18 #include <fstream>
     19 #include <streambuf>
     20 #include <string>
     21 #include <unordered_map>
     22 
     23 #include "tensorflow/core/lib/strings/str_util.h"
     24 #include "tensorflow/core/platform/regexp.h"
     25 
     26 namespace xla {
     27 namespace {
     28 
     29 // Mapping from test name; i.e. MyTest.MyTestCase to platforms on which it is
     30 // disabled - a sequence of regexps.
     31 using ManifestT = std::unordered_map<string, std::vector<string>>;
     32 
     33 ManifestT ReadManifest() {
     34   ManifestT manifest;
     35 
     36   string path = XLA_DISABLED_MANIFEST;
     37   if (path.empty()) {
     38     return manifest;
     39   }
     40 
     41   std::ifstream file_stream(path);
     42   // Note: parens are required to disambiguate vs function decl.
     43   string contents((std::istreambuf_iterator<char>(file_stream)),
     44                   std::istreambuf_iterator<char>());
     45 
     46   std::vector<string> lines = tensorflow::str_util::Split(contents, '\n');
     47   for (string& line : lines) {
     48     auto comment = line.find("//");
     49     if (comment != string::npos) {
     50       line = line.substr(0, comment);
     51     }
     52     if (line.empty()) {
     53       continue;
     54     }
     55     tensorflow::str_util::StripTrailingWhitespace(&line);
     56     std::vector<string> pieces = tensorflow::str_util::Split(line, ' ');
     57     CHECK_GE(pieces.size(), 1);
     58     auto& platforms = manifest[pieces[0]];
     59     for (int64 i = 1; i < pieces.size(); ++i) {
     60       platforms.push_back(pieces[i]);
     61     }
     62   }
     63   return manifest;
     64 }
     65 
     66 }  // namespace
     67 
     68 string PrependDisabledIfIndicated(const string& test_case_name,
     69                                   const string& test_name) {
     70   ManifestT manifest = ReadManifest();
     71 
     72   // First try full match: test_case_name.test_name
     73   // If that fails, try to find just the test_case_name; this would disable all
     74   // tests in the test case.
     75   auto it = manifest.find(
     76       tensorflow::strings::StrCat(test_case_name, ".", test_name));
     77   if (it == manifest.end()) {
     78     it = manifest.find(test_case_name);
     79     if (it == manifest.end()) {
     80       return test_name;
     81     }
     82   }
     83 
     84   // Expect a full match vs. one of the platform regexps to disable the test.
     85   const std::vector<string>& disabled_platforms = it->second;
     86   string platform_string = XLA_PLATFORM;
     87   for (const auto& s : disabled_platforms) {
     88     if (RE2::FullMatch(/*text=*/platform_string, /*re=*/s)) {
     89       return "DISABLED_" + test_name;
     90     }
     91   }
     92 
     93   // We didn't hit in the disabled manifest entries, so don't disable it.
     94   return test_name;
     95 }
     96 
     97 }  // namespace xla
     98