1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Common
import SwiftCWrapper
import TestHelpers
import XCTest
class SwiftCWrapperToolTests: XCTestCase {
var workspace: URL!
var testStressTesterFile: URL!
var testSwiftCFile: URL!
var testSwiftCWrapperFile: URL!
var testFile: URL!
var testInvocationFile: URL!
var errorJson: String!
func testStatus() throws {
typealias WrapperSpec = (compilerExit: Int32, stressTesterExit: Int32, expectedExit: Int32)
let specs: [WrapperSpec] = [
(compilerExit: 2, stressTesterExit: 1, expectedExit: 2),
(compilerExit: 0, stressTesterExit: 1, expectedExit: 1),
(compilerExit: 0, stressTesterExit: 0, expectedExit: 0),
(compilerExit: 0, stressTesterExit: 6, expectedExit: 1)
]
let environment: [String: String] = ["SK_STRESS_SWIFTC": testSwiftCFile.path]
try specs.forEach { spec in
let stdout = spec.stressTesterExit != 0 ? errorJson : nil
ExecutableScript(at: testStressTesterFile,
exitCode: spec.stressTesterExit, stdout: stdout)
ExecutableScript(at: testSwiftCFile,
exitCode: spec.compilerExit)
let singleFileArgs: [String] = [testSwiftCWrapperFile.path, testFile.path]
let wrapper = SwiftCWrapperTool(arguments: singleFileArgs, environment: environment)
XCTAssertNoThrow(XCTAssertEqual(try wrapper.run(), spec.expectedExit))
}
}
func testSilent() throws {
typealias WrapperSpec = (compilerExit: Int32, stressTesterExit: Int32, expectedExit: Int32, silent: Bool)
let specs: [WrapperSpec] = [
(compilerExit: 2, stressTesterExit: 1, expectedExit: 2, silent: false),
(compilerExit: 2, stressTesterExit: 1, expectedExit: 2, silent: true),
(compilerExit: 0, stressTesterExit: 1, expectedExit: 1, silent: false),
(compilerExit: 0, stressTesterExit: 1, expectedExit: 0, silent: true),
(compilerExit: 0, stressTesterExit: 0, expectedExit: 0, silent: true),
(compilerExit: 0, stressTesterExit: 0, expectedExit: 0, silent: false)
]
let environment: [String: String] = ["SK_STRESS_SWIFTC": testSwiftCFile.path]
try specs.forEach { spec in
let stdout = spec.stressTesterExit != 0 ? errorJson : nil
ExecutableScript(at: testStressTesterFile,
exitCode: spec.stressTesterExit, stdout: stdout)
ExecutableScript(at: testSwiftCFile, exitCode: spec.compilerExit)
let singleFileArgs: [String] = [testSwiftCWrapperFile.path, testFile.path]
let environment = environment.merging(["SK_STRESS_SILENT": String(spec.silent)]) { _, new in new }
let wrapper = SwiftCWrapperTool(arguments: singleFileArgs, environment: environment)
XCTAssertNoThrow(XCTAssertEqual(try wrapper.run(), spec.expectedExit))
}
}
func testStressTesterOperationQueue() throws {
try XCTSkipIf(true, "Failing non-deterministically. Disabling until we have time to investigate - rdar://100970606")
class TestOperation: Operation {
var waitCount: Int
init(waitCount: Int) {
self.waitCount = waitCount
}
override func main() {
while !isCancelled, waitCount > 0 {
usleep(100)
waitCount -= 1
}
}
}
let first = TestOperation(waitCount: 0)
let second = TestOperation(waitCount: 10)
let third = TestOperation(waitCount: 20)
let fourth = TestOperation(waitCount: 30)
StressTesterOperationQueue(operations: [first, second, third, fourth], maxWorkers: 2, completionHandler: { _, finishedOp, _, _ in
// cancel later operations when the second operation completes
return finishedOp !== second
}).waitUntilFinished()
XCTAssertFalse(first.isCancelled, "first was cancelled")
XCTAssertFalse(second.isCancelled, "second was cancelled")
XCTAssertTrue(third.isCancelled, "third wasn't cancelled")
XCTAssertTrue(fourth.isCancelled, "fourth wasn't cancelled")
}
func testSwiftFileHeuristic() {
func getSwiftFiles(from list: [String]) -> [String] {
let wrapper: SwiftCWrapper = SwiftCWrapper(
swiftcArgs: list, swiftcPath: "", stressTesterPath: "",
astBuildLimit: nil, requestDurationsOutputFile: nil,
rewriteModes: [], requestKinds: Set(),
conformingMethodTypes: nil, extraCodeCompleteOptions: [], ignoreIssues: false, issueManager: nil,
maxJobs: nil, dumpResponsesPath: nil, failFast: false,
suppressOutput: false)
return wrapper.swiftFiles.map { (file, _) in file }
}
let dirPath = workspace.appendingPathComponent("ConfusinglyNamedDir.swift", isDirectory: true).path
let blacklistedDir = workspace.appendingPathComponent("SourcePackages/checkouts/SomeSubRepo", isDirectory: true).path
let blacklistedFile = workspace.appendingPathComponent("SourcePackages/checkouts/SomeSubRepo/file.swift", isDirectory: false).path
try! FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: false, attributes: nil)
try! FileManager.default.createDirectory(atPath: blacklistedDir, withIntermediateDirectories: true, attributes: nil)
guard FileManager.default.createFile(atPath: blacklistedFile, contents: nil) else { fatalError() }
XCTAssertEqual(getSwiftFiles(from: [testFile.path,
"/made-up.swift",
"unrelated/path",
blacklistedFile,
dirPath]),
[testFile.path])
}
func testEnvParsing() {
ExecutableScript(at: testSwiftCFile, exitCode: 0)
let tester = ExecutableScript(at: testStressTesterFile, exitCode: 0,
recordInvocationIn: testInvocationFile)
let defaultEnvironment: [String: String] = [
"SK_STRESS_SWIFTC": testSwiftCFile.path,
"SK_STRESS_TEST": testStressTesterFile.path,
]
// Check the produced invocations with default settings
let singleFileArgs: [String] = [testSwiftCWrapperFile.path, testFile.path]
XCTAssertNoThrow(XCTAssertEqual(try SwiftCWrapperTool(arguments: singleFileArgs, environment: defaultEnvironment).run(), 0))
let defaultInvocations = tester.retrieveInvocations()
XCTAssertEqual(defaultInvocations.count, 3)
assertInvocationsMatch(invocations: defaultInvocations,
rewriteModes: [.none, .concurrent, .insideOut])
// Check custom request kinds and rewrite modes are propagated through correctly
let customRequestKinds = [RequestKind.cursorInfo, RequestKind.rangeInfo]
let customEnvironment = defaultEnvironment.merging([
"SK_STRESS_REQUESTS": customRequestKinds
.map({ "\($0.rawValue)" }).joined(separator: " "),
"SK_STRESS_REWRITE_MODES": [RewriteMode.basic, RewriteMode.insideOut]
.map({ "\($0.rawValue)" }).joined(separator: " "),
"SK_STRESS_CONFORMING_METHOD_TYPES": "s:SomeUSR s:OtherUSR s:ThirdUSR"
], uniquingKeysWith: { _, new in new })
XCTAssertNoThrow(XCTAssertEqual(try SwiftCWrapperTool(arguments: singleFileArgs, environment: customEnvironment).run(), 0))
let customInvocations = tester.retrieveInvocations()
XCTAssertEqual(customInvocations.count, 2)
assertInvocationsMatch(invocations: customInvocations,
rewriteModes: [.basic, .insideOut],
requestKinds: customRequestKinds)
}
func testIssueManager() {
let xfail = ExpectedIssue(
applicableConfigs: ["main"], issueUrl: "<issue-url>",
path: "*/foo/bar.swift", modification: "unmodified",
issueDetail: .editorReplaceText(offset: 42, length: 0, text: nil)
)
let xfail2 = ExpectedIssue(
applicableConfigs: ["main"], issueUrl: "<issue-url",
path: "*/foo/bar.swift", modification: "unmodified",
issueDetail: .stressTesterCrash(status: 2, arguments: "*concurrent*"))
let document1 = DocumentInfo(path: "/baz/foo/bar.swift", modification: nil)
let request1 = RequestInfo.editorReplaceText(document: document1, offset: 42, length: 0, text: ".")
let error1 = SourceKitError.crashed(request: request1)
let issue1 = StressTesterIssue.failed(sourceKitError: error1, arguments: "")
let request2 = RequestInfo.editorReplaceText(document: document1, offset: 42, length: 2, text: "hello")
let error2 = SourceKitError.crashed(request: request2)
let issue2 = StressTesterIssue.failed(sourceKitError: error2, arguments: "")
let document2 = DocumentInfo(path: "/baz/bar.swift", modification: nil)
let request3 = RequestInfo.editorReplaceText(document: document2, offset: 42, length: 0, text: ".")
let error3 = SourceKitError.crashed(request: request3)
let issue3 = StressTesterIssue.failed(sourceKitError: error3, arguments: "")
let error4 = SourceKitError.failed(.errorResponse, request: request1, response: "foo")
let issue4 = StressTesterIssue.failed(sourceKitError: error4, arguments: "")
let issue5 = StressTesterIssue.errored(status: 2, file: "/bob/foo/bar.swift",
arguments: "--rewrite-mode concurrent /bob/foo/bar.swift -- /bob/foo/bar.swift")
let issue6 = StressTesterIssue.errored(status: 2,
file: "/bob/foo/bar.swift",
arguments: "--rewrite-mode basic /bob/foo/bar.swift -- /bob/foo/bar.swift")
XCTAssertTrue(xfail.matches(issue1))
XCTAssertFalse(xfail.matches(issue2))
XCTAssertFalse(xfail.matches(issue3))
XCTAssertTrue(xfail.matches(issue4))
XCTAssertTrue(xfail2.matches(issue5))
XCTAssertFalse(xfail2.matches(issue6))
}
override func setUp() {
super.setUp()
workspace = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent("StressTesterToolTests", isDirectory: true)
try? FileManager.default.removeItem(at: workspace)
try! FileManager.default.createDirectory(at: workspace, withIntermediateDirectories: false)
testSwiftCWrapperFile = workspace
.appendingPathComponent("sk-swiftc-wrapper", isDirectory: false)
testSwiftCFile = workspace
.appendingPathComponent("swiftc", isDirectory: false)
testStressTesterFile = workspace
.appendingPathComponent("sk-stress-test", isDirectory: false)
testFile = workspace
.appendingPathComponent("test.swift", isDirectory: false)
testInvocationFile = workspace
.appendingPathComponent("invocations.txt", isDirectory: false)
errorJson = """
{"message": "detected", "error": {\
"error": "timedOut",\
"request": {\
"document": {"path":"\(testFile.path)"},\
"offset": 5,\
"args": ["\(testFile.path)"],\
"request": "cursorInfo"\
}\
}}
"""
FileManager.default.createFile(atPath: testFile.path, contents: """
func square(_ x: Int) -> Int {
return x * x
}
print(square(9))
""".data(using: .utf8))
}
override func tearDown() {
super.tearDown()
try? FileManager.default.removeItem(at: workspace)
}
private func assertInvocationsMatch(invocations: [Substring],
rewriteModes: [RewriteMode],
requestKinds: [RequestKind] = RequestKind.ideRequests) {
for invocation in invocations {
XCTAssertTrue(invocation.contains("--format json"),
"Missing json format in '\(invocation)'")
XCTAssertTrue(invocation.contains("--page 1/1"),
"Missing page in '\(invocation)'")
var foundModes = [RewriteMode]()
for rewriteMode in RewriteMode.allCases {
if invocation.contains("--rewrite-mode \(rewriteMode)") {
foundModes.append(rewriteMode)
}
}
XCTAssertEqual(foundModes.count, 1,
"Found multiple rewrite modes \(foundModes) in '\(invocation)'")
XCTAssertTrue(rewriteModes.contains(foundModes[0]),
"Expected a rewrite mode matching \(rewriteModes) in '\(invocation)'")
var foundRequests = [RequestKind]()
for request in RequestKind.allCases {
if invocation.contains("--request \(request)") {
foundRequests.append(request)
}
}
XCTAssertTrue(Set(foundRequests)
.intersection(requestKinds).count == requestKinds.count,
"Expected only request kinds \(requestKinds) in '\(invocation)'")
XCTAssertTrue(invocation.contains("--swiftc \(testSwiftCFile.path)"),
"Incorrect swiftc path in '\(invocation)'")
XCTAssertTrue(invocation.contains("\(testFile.path) -- \(testFile.path)"),
"Incorrect file paths in '\(invocation)'")
}
}
}
|