 import XCTest
 import TestUtilities
 
+private func executableName(_ name: String) -> String {
+#if os(Windows)
+  if name.count > 4, name.suffix(from: name.index(name.endIndex, offsetBy: -4)) == ".exe" {
+    return name
+  }
+  return "\(name).exe"
+#else
+  return name
+#endif
+}
+
 final class SwiftDriverTests: XCTestCase {
 
   private var envWithFakeSwiftHelp: [String: String] {

    XCTAssertTrue(matchTemporary(plannedJobs[0].outputs.first!.file, "foo.o"))
    XCTAssertEqual(plannedJobs[1].outputs.count, 1)
    XCTAssertTrue(matchTemporary(plannedJobs[1].outputs.first!.file, "bar.o"))
    XCTAssertTrue(plannedJobs[2].tool.name.contains(driver1.targetTriple.isDarwin ? "ld" : "clang"))
    XCTAssertEqual(plannedJobs[2].outputs.count, 1)
    XCTAssertEqual(plannedJobs[2].outputs.first!.file, VirtualPath.relative(RelativePath("Test")))

    // Forwarding of arguments.
    var driver2 = try Driver(args: ["swiftc", "-color-diagnostics", "foo.swift", "bar.swift", "-working-directory", "/tmp", "-api-diff-data-file", "diff.txt", "-Xfrontend", "-HI", "-no-color-diagnostics", "-g"])

    XCTAssertEqual(output,
    """
    digraph Jobs {
      "emitModule (\(executableName("swift-frontend")))" [style=bold];
      "test.swift" [fontsize=12];
      "test.swift" -> "emitModule (\(executableName("swift-frontend")))" [color=blue];
      "test.swiftmodule" [fontsize=12];
      "emitModule (\(executableName("swift-frontend")))" -> "test.swiftmodule" [color=green];
      "test.swiftdoc" [fontsize=12];
      "emitModule (\(executableName("swift-frontend")))" -> "test.swiftdoc" [color=green];
      "test.abi.json" [fontsize=12];
      "emitModule (\(executableName("swift-frontend")))" -> "test.abi.json" [color=green];
      "compile (\(executableName("swift-frontend")))" [style=bold];
      "test.swift" -> "compile (\(executableName("swift-frontend")))" [color=blue];
      "test-1.o" [fontsize=12];
      "compile (\(executableName("swift-frontend")))" -> "test-1.o" [color=green];
      "link (\(linkerDriver))" [style=bold];
      "test-1.o" -> "link (\(linkerDriver))" [color=blue];
      "\(executableName("test"))" [fontsize=12];
      "link (\(linkerDriver))" -> "\(executableName("test"))" [color=green];
    }

    """)
