File: StressTesterToolTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (213 lines) | stat: -rw-r--r-- 8,250 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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 StressTester
import TestHelpers
import XCTest

class StressTesterToolTests: XCTestCase {
  var workspace: URL!
  var testFile: URL!
  var swiftcPath: String!

  func testCommandLine() {
    let valid: [String] = [testFile.path, "--", testFile.path]

    // No source file
    XCTAssertThrowsError(try StressTesterTool.parse([]))

    // No compiler args
    XCTAssertThrowsError(try StressTesterTool.parse([testFile.path]))

    // Compiler args missing file
    // FIXME: Add back in when validate is back to normal
    // XCTAssertThrowsError(try StressTesterTool.parse([testFile.path, "--", "anything"]))

    // Defaults
    StressTesterToolTests.assertParse(valid) { defaults in
      XCTAssertEqual(defaults.rewriteMode, .none)
      XCTAssertEqual(defaults.format, .humanReadable)
      XCTAssertEqual(defaults.limit, nil)
      XCTAssertEqual(defaults.page, Page())
      XCTAssertEqual(defaults.requests, [.ide])
      XCTAssertEqual(defaults.dryRun, false)
      XCTAssertEqual(defaults.reportResponses, false)
      XCTAssertEqual(defaults.conformingMethodsTypeList, ["s:SQ", "s:SH"])
      // FIXME: Change to swiftcPath when validate is back to normal
      XCTAssertEqual(defaults.swiftc, nil)
      XCTAssertEqual(defaults.file, testFile)
      XCTAssertEqual(defaults.compilerArgs, [CompilerArg(testFile!.path)])
    }

    let allLong: [String] = [
      "--rewrite-mode", "basic",
      "--format", "json",
      "--limit", "1",
      "--page", "2/5",
      "--request", "CursorInfo", "--request", "CODECOMPLETE",
      "--dry-run",
      "--report-responses",
      "--type-list-item", "foo", "--type-list-item", "bar",
      "--temp-dir", workspace.path,
      "--swiftc", swiftcPath,
      testFile.path, "--", testFile.path
    ]
    let allShort: [String] = [
      "-m", "basic",
      "-f", "json",
      "-l", "1",
      "-p", "2/5",
      "-r", "CursorInfo", "-r", "CODECOMPLETE",
      "-d",
      "--report-responses", // no short
      "-t", "foo", "-t", "bar",
      "--temp-dir", workspace.path, // no short
      "-s", swiftcPath,
      testFile.path, "--", testFile.path
    ]
    let assertValid = { [workspace, swiftcPath, testFile] (args: [String]) in
      StressTesterToolTests.assertParse(args) { tool in
        XCTAssertEqual(tool.rewriteMode, .basic)
        XCTAssertEqual(tool.format, .json)
        XCTAssertEqual(tool.limit, 1)
        XCTAssertEqual(tool.page, Page(2, of: 5))
        XCTAssertEqual(tool.requests, [.cursorInfo, .codeComplete])
        XCTAssertEqual(tool.dryRun, true)
        XCTAssertEqual(tool.reportResponses, true)
        XCTAssertEqual(tool.conformingMethodsTypeList, ["foo", "bar"])
        XCTAssertEqual(tool.tempDir, workspace)
        XCTAssertEqual(tool.swiftc, swiftcPath)
        XCTAssertEqual(tool.file, testFile!)
        XCTAssertEqual(tool.compilerArgs, [CompilerArg(testFile!.path)])
      }
    }
    assertValid(allLong)
    assertValid(allShort)

    // Unknown flag
    XCTAssertThrowsError(try StressTesterTool.parse(["--not-an-option"] + valid))

    // Missing
    XCTAssertThrowsError(try StressTesterTool.parse(["--format"] + valid))

    // Unknown values
    XCTAssertThrowsError(try StressTesterTool.parse(["--format", "blah"] + valid))
    XCTAssertThrowsError(try StressTesterTool.parse(["--request", "Unknown"] + valid))
  }

  func testDumpResponses() {
    var responses = [SourceKitResponseData]()
    let options = StressTesterOptions(requests: [.codeComplete],
                                      rewriteMode: .none,
                                      conformingMethodsTypeList: [],
                                      page: Page(),
                                      offsetFilter: 11,
                                      tempDir: workspace,
                                      responseHandler: { responseData in
                                        responses.append(responseData)
    })

    let tester = StressTester(options: options)
    let errors = tester.run(swiftc: swiftcPath,
                            compilerArgs: CompilerArgs(
                              for: testFile,
                                 args: [CompilerArg(testFile.path)],
                                 tempDir: workspace))
    XCTAssert(errors.isEmpty, "no sourcekitd crashes in test program")
    XCTAssertFalse(responses.isEmpty, "produces responses")
    XCTAssertTrue(responses.allSatisfy { response in
      if case .codeCompleteOpen(document: _, offset: let offset, args: _) = response.request {
        return offset == 11
      }
      return false
    }, "request filter is respected")
    XCTAssertFalse(responses.allSatisfy { $0.results.isEmpty }, "responses have results")
  }

  func testModuleRequest() {
    let swiftc = ExecutableScript(
      at: workspace.appendingPathComponent("swiftc", isDirectory: false),
      exitCode: 0,recordInvocationIn:
        workspace.appendingPathComponent("invocation.txt", isDirectory: false))

    // -emit-object and -c must be removed since they would cause skipping
    // function bodies to fail
    let args = ["-emit-object", testFile.path].map({ CompilerArg($0) })
    let options = StressTesterOptions(requests: [.testModule],
                                      rewriteMode: .none,
                                      conformingMethodsTypeList: [],
                                      page: Page(),
                                      offsetFilter: nil,
                                      tempDir: workspace)

    let tester = StressTester(options: options)
    // Expect interface request to fail since no module will be generated
    let errors = tester.run(swiftc: swiftc.file.path,
                            compilerArgs: CompilerArgs(
                              for: testFile,
                                 args: args,
                                 tempDir: workspace))
    XCTAssert(errors.count == 1)

    let invocations = swiftc.retrieveInvocations()
    let expectedInvocation = """
      -whole-module-optimization \
      -Xfrontend -experimental-allow-module-with-compiler-errors \
      -Xfrontend -experimental-skip-all-function-bodies \
      -emit-module -module-name TestModForStressTester -emit-module-path \
      \(workspace.appendingPathComponent("TestModForStressTester.swiftmodule").path) -
      """
    XCTAssertEqual(1, invocations.count)
    XCTAssertEqual(expectedInvocation, String(invocations[0]),
                  "incorrect args sent to swiftc")
  }

  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)

    testFile = workspace
      .appendingPathComponent("test.swift")

    FileManager.default.createFile(atPath: testFile.path, contents: """
      func square(_ x: Int) -> Int {
        return x * x
      }
      print(square(9))
      """.data(using: .utf8))

    swiftcPath = pathFromXcrun(for: "swiftc")
  }

  override func tearDown() {
    super.tearDown()
    try? FileManager.default.removeItem(at: workspace)
  }

  private static func assertParse(_ args: [String],
                                  file: StaticString = #file, line: UInt = #line,
                                  closure: (StressTesterTool) throws -> Void) {
    do {
      let parsed = try StressTesterTool.parse(args)
      try closure(parsed)
    } catch {
      let message = StressTesterTool.message(for: error)
      XCTFail("\"\(message)\" — \(error)", file: (file), line: line)
    }
  }
}