<commit_before>// Copyright 2016 The Tulsi Authors. All rights reserved.
//
// 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.

import XCTest
@testable import TulsiGenerator


/// Base class for test cases utilizing an external Bazel instance.
/// Command line options:
///   -test_bazel: (Required) Path to the Bazel binary to use for testing.
///   -testBazelStartupOptions <string>: Options to be passed to Bazel before the "build" command.
///   -testBazelBuildOptions <string>: Options to be passed to Bazel after the "build" command.
///   -keep_test_output (0 or 1): Retain the test's temporary workspace directory rather than
///       cleaning it up after the test completes.
///   -use_hosted_workspace <path>: Path to a Bazel workspace inside which the test files will be
///       executed. A WORKSPACE file must be present in the given directory.
class BazelIntegrationTestCase: XCTestCase {

  var bazelURL: NSURL! = nil
  var bazelStartupOptions = [String]()
  var bazelBuildOptions = [String]()
  var workspaceRootURL: NSURL! = nil
  var packagePathFetcher: BazelWorkspacePathInfoFetcher! = nil
  var localizedMessageLogger: DirectLocalizedMessageLogger! = nil

  private var pathsToCleanOnTeardown = Set<NSURL>()

  override func setUp() {
    super.setUp()

    let userDefaults = NSUserDefaults.standardUserDefaults()
    guard let bazelPath = userDefaults.stringForKey("test_bazel") else {
      XCTFail("This test must be launched with test_bazel set to a path to Bazel " +
                  "(e.g., via -test_bazel as a command-line argument)")
      return
    }
    bazelURL = NSURL(fileURLWithPath: bazelPath)

    let commandLineSplitter = CommandLineSplitter()
    if let startupOptions = userDefaults.stringForKey("testBazelStartupOptions") {
      guard let splitOptions = commandLineSplitter.splitCommandLine(startupOptions) else {
        XCTFail("Failed to split bazelStartupOptions '\(startupOptions)'")
        return
      }
      bazelStartupOptions = splitOptions
    }
    if let buildOptions = userDefaults.stringForKey("testBazelBuildOptions") {
      guard let splitOptions = commandLineSplitter.splitCommandLine(buildOptions) else {
        XCTFail("Failed to split bazelBuildOptions '\(buildOptions)'")
        return
      }
      bazelBuildOptions = splitOptions
    }

    if let hostedWorkspaceDirectory = userDefaults.stringForKey("use_hosted_workspace") {
      workspaceRootURL = NSURL(fileURLWithPath: hostedWorkspaceDirectory,
                                         isDirectory: true)
    } else {
      let globalTempDir = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
      let dirName = "tulsi_\(NSUUID().UUIDString)"
      workspaceRootURL = globalTempDir.URLByAppendingPathComponent(dirName, isDirectory: true)

      installWorkspaceFile()
    }

    guard let workspaceRootURL = workspaceRootURL else {
      fatalError("Failed to find workspaceRootURL.")
    }

    localizedMessageLogger = DirectLocalizedMessageLogger()
    localizedMessageLogger.startLogging()
    packagePathFetcher = BazelWorkspacePathInfoFetcher(bazelURL: bazelURL,
                                                       workspaceRootURL: workspaceRootURL,
                                                       localizedMessageLogger: localizedMessageLogger)
  }

  override func tearDown() {
    super.tearDown()
    cleanCreatedFiles()
    packagePathFetcher = nil<commit_msg>Make integration tests more consistent. Always use '--[]rc=/dev/null' as the bazel startup option.<commit_after>// Copyright 2016 The Tulsi Authors. All rights reserved.
//
// 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.

import XCTest
@testable import TulsiGenerator


/// Base class for test cases utilizing an external Bazel instance.
/// Command line options:
///   -test_bazel: (Required) Path to the Bazel binary to use for testing.
///   -testBazelStartupOptions <string>: Options to be passed to Bazel before the "build" command.
///   -testBazelBuildOptions <string>: Options to be passed to Bazel after the "build" command.
///   -keep_test_output (0 or 1): Retain the test's temporary workspace directory rather than
///       cleaning it up after the test completes.
///   -use_hosted_workspace <path>: Path to a Bazel workspace inside which the test files will be
///       executed. A WORKSPACE file must be present in the given directory.
class BazelIntegrationTestCase: XCTestCase {

  var bazelURL: NSURL! = nil
  var bazelStartupOptions = [String]()
  var bazelBuildOptions = [String]()
  var workspaceRootURL: NSURL! = nil
  var packagePathFetcher: BazelWorkspacePathInfoFetcher! = nil
  var localizedMessageLogger: DirectLocalizedMessageLogger! = nil

  private var pathsToCleanOnTeardown = Set<NSURL>()

  override func setUp() {
    super.setUp()

    let userDefaults = NSUserDefaults.standardUserDefaults()
    guard let bazelPath = userDefaults.stringForKey("test_bazel") else {
      XCTFail("This test must be launched with test_bazel set to a path to Bazel " +
                  "(e.g., via -test_bazel as a command-line argument)")
      return
    }
    bazelURL = NSURL(fileURLWithPath: bazelPath)

    let commandLineSplitter = CommandLineSplitter()
    if let startupOptions = userDefaults.stringForKey("testBazelStartupOptions") {
      guard let splitOptions = commandLineSplitter.splitCommandLine(startupOptions) else {
        XCTFail("Failed to split bazelStartupOptions '\(startupOptions)'")
        return
      }
      bazelStartupOptions = splitOptions
    }
    if let buildOptions = userDefaults.stringForKey("testBazelBuildOptions") {
      guard let splitOptions = commandLineSplitter.splitCommandLine(buildOptions) else {
        XCTFail("Failed to split bazelBuildOptions '\(buildOptions)'")
        return
      }
      bazelBuildOptions = splitOptions
    }

    if let hostedWorkspaceDirectory = userDefaults.stringForKey("use_hosted_workspace") {
      workspaceRootURL = NSURL(fileURLWithPath: hostedWorkspaceDirectory,
                                         isDirectory: true)
    } else {
      let globalTempDir = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
      let dirName = "tulsi_\(NSUUID().UUIDString)"
      workspaceRootURL = globalTempDir.URLByAppendingPathComponent(dirName, isDirectory: true)

      installWorkspaceFile()
    }

    // Prevent any custom --blazerc startup option to be specified. It should always be /dev/null.
    for startupOption in bazelStartupOptions {
      if (startupOption.hasPrefix("--blazerc=") && startupOption != "--blazerc=/dev/null") {
        fatalError("bazelStartupOptions includes custom blazerc, which is not allowed '\(startupOption)'")
      }
    }
    bazelStartupOptions.append("--blazerc=/dev/null")

    guard let workspaceRootURL = workspaceRootURL else {
      fatalError("Failed to find workspaceRootURL.")
    }

    localizedMessageLogger = DirectLocalizedMessageLogger()
    localizedMessageLogger.startLogging()
    packagePathFetcher = BazelWorkspacePathInfoFetcher(bazelURL: bazelURL,
                                                       workspaceRootURL: workspaceRootURL,
                                                       localizedMessageLogger: localizedMessageLogger)
  }

  override func tearDown() {
    super.tearDown()
    cleanCreatedFiles()
    packagePathFetcher = nil
{'fraction_non_alphanumeric': 0.08044879446168536, 'fraction_numerical': 0.0023872045834328003, 'mean_word_length': 3.231818181818182, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '417843', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2236, 'n_words': 715}