       return context.range(of: target) != nil
     case .member(context: let context, name: let name):
       return context.range(of: target) != nil || name == target
-    }
-  }
-}
-
-// MARK: - Incremental test perturbation helpers
-extension IncrementalCompilationTests {
-  private func touch(_ name: String) {
-    print("*** touching \(name) ***", to: &stderrStream); stderrStream.flush()
-    let (path, contents) = try! XCTUnwrap(inputPathsAndContents.filter {$0.0.pathString.contains(name)}.first)
-    try! localFileSystem.writeFileContents(path) { $0 <<< contents }
-  }
-
-  private func replace(contentsOf name: String, with replacement: String ) {
-    print("*** replacing \(name) ***", to: &stderrStream); stderrStream.flush()
-    let path = inputPath(basename: name)
-    let previousContents = try! localFileSystem.readFileContents(path).cString
-    try! localFileSystem.writeFileContents(path) { $0 <<< replacement }
-    let newContents = try! localFileSystem.readFileContents(path).cString
-    XCTAssert(previousContents != newContents, "\(path.pathString) unchanged after write")
-    XCTAssert(replacement == newContents, "\(path.pathString) failed to write")
-  }
-
-  private func write(_ contents: String, to basename: String) {
-    print("*** writing \(contents) to \(basename)")
-    try! localFileSystem.writeFileContents(inputPath(basename: basename)) {
-      $0 <<< contents
     }
   }
 }

  }

  func testIncremental(checkDiagnostics: Bool) throws {
    try tryInitial(checkDiagnostics: checkDiagnostics)
#if true // sometimes want to skip for debugging
    try tryNoChange(checkDiagnostics: checkDiagnostics)
    try tryTouchingOther(checkDiagnostics: checkDiagnostics)
    try tryTouchingBoth(checkDiagnostics: checkDiagnostics)
#endif
    try tryReplacingMain(checkDiagnostics: checkDiagnostics)
  }

  // FIXME: Expect failure in Linux in CI just as testIncrementalDiagnostics
  func testAlwaysRebuildDependents() throws {
#if !os(Linux)
    try tryInitial(checkDiagnostics: true)
    try tryTouchingMainAlwaysRebuildDependents(checkDiagnostics: true)
#endif
  }


  /// Ensure that the mod date of the input comes back exactly the same via the build-record.
  /// Otherwise the up-to-date calculation in `IncrementalCompilationState` will fail.
  func testBuildRecordDateAccuracy() throws {
    try buildInitialState()
    try (1...10).forEach { n in
      try checkNullBuild(checkDiagnostics: true)
    }
  }
}

// MARK: - Test adding an input
extension IncrementalCompilationTests {

  func testAddingInput() throws {
#if !os(Linux)
  try testAddingInput(newInput: "another", defining: "nameInAnother")
#endif
  }

  /// Test the addition of an input file
  ///
  /// - Parameters:
  ///   - newInput: basename without extension of new input file
  ///   - topLevelName: a new top level name defined in the new input
  private func testAddingInput(newInput: String, defining topLevelName: String
  ) throws {
    let initial = try buildInitialState(checkDiagnostics: true).moduleDependencyGraph()
    initial.ensureOmits(sourceBasenameWithoutExt: newInput)
    initial.ensureOmits(name: topLevelName)

    write("func \(topLevelName)() {}", to: newInput)
    let newInputsPath = inputPath(basename: newInput)
    OutputFileMapCreator.write(module: module,
                               inputPaths: inputPathsAndContents.map {$0.0} + [newInputsPath],
                               derivedData: derivedDataPath,
                               to: OFM)
    try checkReactionToAddingInput(newInput: newInput, definingTopLevel: topLevelName)
    try checkRestorationOfIncrementalityAfterAddition(newInput: newInput, definingTopLevel: topLevelName)
  }
}

// MARK: - Incremental test stages
extension IncrementalCompilationTests {
  /// Setup the initial post-build state.
  ///
  /// - Parameters:
  ///   - checkDiagnostics: If true verify the diagnostics
  ///   - extraArguments: Additional command-line arguments
  /// - Returns: The `Driver` object
  @discardableResult
  private func buildInitialState(
    checkDiagnostics: Bool = false,
    extraArguments: [String] = []
  ) throws -> Driver {
    try doABuild(
      "initial",
