-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add snippet-build tool The `snippet-build` tool crawls the `Snippets` subdirectory of a Swift Package if present, parses the `.swift` files within, and creates snippets and snippet groups. If there are any snippets, they are serialized into a Symbol Graph JSON file into an output directory. rdar://89650557 * Use 'Snippets' as the default group name Group names with spaces cause problems with path components / URLs. * Pass snippets directory to `snippet-build` Instead of expecting a particular directory name, just in case. Add some logging to the tool. * Call `snippet-build` on snippet files Forward generated Symbol Graph JSON to the docc invocation. * Pass snippets directory instead of package path Just in case. * Temporarily remove ArgumentParser and TSC dependencies Plugins can't have their own dependencies because they currently share the same dependency graph as the clients opting into the plugin. rdar://89789701 * Refactor snippet generation * Add tests for SnippetBuilder * Fix typo "ot" -> "to" Co-authored-by: Ethan Kusters <[email protected]> * Use @main for snippet-build * Remove local dependencies Not necessary for swift-docc-plugin builds at this time. * Remove debug logging from SnippetBuildCommand * Add unit tests for snippet parsing * Add integration tests for building projects with snippets * [Snippets] Logic: Use total snippet count to early-return It's enough to check the total number of snippets rather than checking the groups top-down. * [Snippets] Use trimmingCharacters for newline trimming When trimming leading and trailing newlines, use the existing `trimmingCharacters(in:)` API. NFC. * Fix snippet builder tests * Fix pipe output deadlock * Use shared SwiftPM cache when building in integration tests Co-authored-by: Ethan Kusters <[email protected]>
- Loading branch information
1 parent
c4f7b07
commit 859caac
Showing
22 changed files
with
1,502 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
IntegrationTests/Tests/Fixtures/PackageWithSnippets/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// swift-tools-version: 5.6 | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 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 Swift project authors | ||
|
||
import Foundation | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "PackageWithSnippets", | ||
targets: [ | ||
.target(name: "Library"), | ||
] | ||
) | ||
|
||
// We only expect 'swift-docc-plugin' to be a sibling when this package | ||
// is running as part of a test. | ||
// | ||
// This allows the package to compile outside of tests for easier | ||
// test development. | ||
if FileManager.default.fileExists(atPath: "../swift-docc-plugin") { | ||
package.dependencies += [ | ||
.package(path: "../swift-docc-plugin"), | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
IntegrationTests/Tests/Fixtures/PackageWithSnippets/Sources/Library/Library.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 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 Swift project authors | ||
|
||
/// This is BestStruct's documentation. | ||
/// | ||
/// Is best. | ||
public struct BestStruct { | ||
public func best() {} | ||
} |
17 changes: 17 additions & 0 deletions
17
IntegrationTests/Tests/Fixtures/PackageWithSnippets/_Snippets/TestTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 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 Swift project authors | ||
|
||
//! Create a foo. | ||
import Library | ||
|
||
let best = BestStruct() | ||
best.best() | ||
|
||
// MARK: Hide | ||
|
||
print(best) |
136 changes: 136 additions & 0 deletions
136
IntegrationTests/Tests/SnippetDocumentationGenerationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 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 Swift project authors | ||
|
||
import XCTest | ||
|
||
final class SnippetDocumentationGenerationTests: XCTestCase { | ||
func testGenerateDocumentationForPackageWithSnippets() throws { | ||
let result = try swiftPackage( | ||
"generate-documentation", "--enable-experimental-snippet-support", | ||
workingDirectory: try setupTemporaryDirectoryForFixture(named: "PackageWithSnippets") | ||
) | ||
|
||
result.assertExitStatusEquals(0) | ||
XCTAssertEqual(result.referencedDocCArchives.count, 1) | ||
|
||
let doccArchiveURL = try XCTUnwrap(result.referencedDocCArchives.first) | ||
|
||
let dataDirectoryContents = try filesIn(.dataSubdirectory, of: doccArchiveURL) | ||
|
||
XCTAssertEqual( | ||
Set(dataDirectoryContents.map(\.lastTwoPathComponents)), | ||
[ | ||
"documentation/library.json", | ||
"library/beststruct.json", | ||
"beststruct/best().json", | ||
] | ||
) | ||
|
||
let subDirectoriesOfSymbolGraphDirectory = try FileManager.default.contentsOfDirectory( | ||
at: result.symbolGraphsDirectory, | ||
includingPropertiesForKeys: nil | ||
) | ||
|
||
XCTAssertEqual( | ||
Set(subDirectoriesOfSymbolGraphDirectory.map(\.lastTwoPathComponents)), | ||
[ | ||
"symbol-graphs/snippet-symbol-graphs", | ||
"symbol-graphs/unified-symbol-graphs", | ||
] | ||
) | ||
} | ||
|
||
func testGenerateDocumentationForPackageWithSnippetsWithoutExperimentalFlag() throws { | ||
let result = try swiftPackage( | ||
"generate-documentation", | ||
workingDirectory: try setupTemporaryDirectoryForFixture(named: "PackageWithSnippets") | ||
) | ||
|
||
result.assertExitStatusEquals(0) | ||
XCTAssertEqual(result.referencedDocCArchives.count, 1) | ||
|
||
let doccArchiveURL = try XCTUnwrap(result.referencedDocCArchives.first) | ||
|
||
let dataDirectoryContents = try filesIn(.dataSubdirectory, of: doccArchiveURL) | ||
|
||
XCTAssertEqual( | ||
Set(dataDirectoryContents.map(\.lastTwoPathComponents)), | ||
[ | ||
"documentation/library.json", | ||
"library/beststruct.json", | ||
"beststruct/best().json", | ||
] | ||
) | ||
|
||
XCTAssertFalse( | ||
FileManager.default.fileExists(atPath: result.symbolGraphsDirectory.path), | ||
"Unified symbol graph directory created when experimental snippet support flag was not passed." | ||
) | ||
} | ||
|
||
func testPreviewDocumentationWithSnippets() throws { | ||
let outputDirectory = try temporaryDirectory().appendingPathComponent("output") | ||
|
||
let port = try getAvailablePort() | ||
|
||
let process = try swiftPackageProcess( | ||
[ | ||
"--disable-sandbox", | ||
"--allow-writing-to-directory", outputDirectory.path, | ||
"preview-documentation", | ||
"--port", port, | ||
"--output-path", outputDirectory.path, | ||
"--enable-experimental-snippet-support" | ||
], | ||
workingDirectory: try setupTemporaryDirectoryForFixture(named: "PackageWithSnippets") | ||
) | ||
|
||
let outputPipe = Pipe() | ||
process.standardOutput = outputPipe | ||
process.standardError = outputPipe | ||
|
||
try process.run() | ||
|
||
var previewServerHasStarted: Bool { | ||
// We expect docc to emit a `data` directory at the root of the | ||
// given output path when it's finished compilation. | ||
// | ||
// At this point we can expect that the preview server will start imminently. | ||
return FileManager.default.fileExists( | ||
atPath: outputDirectory.appendingPathComponent("data", isDirectory: true).path | ||
) | ||
} | ||
|
||
let previewServerHasStartedExpectation = expectation(description: "Preview server started.") | ||
|
||
let checkPreviewServerTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { timer in | ||
if previewServerHasStarted { | ||
previewServerHasStartedExpectation.fulfill() | ||
timer.invalidate() | ||
} | ||
} | ||
|
||
wait(for: [previewServerHasStartedExpectation], timeout: 15) | ||
checkPreviewServerTimer.invalidate() | ||
|
||
guard process.isRunning else { | ||
XCTFail( | ||
""" | ||
Preview server failed to start. | ||
Process output: | ||
\(try outputPipe.asString() ?? "nil") | ||
""" | ||
) | ||
return | ||
} | ||
|
||
// Send an interrupt to the SwiftPM parent process | ||
process.interrupt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.