Skip to content

Fix swift package init --help printout #3407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ let package = Package(
dependencies: ["Build", "SPMTestSupport"]),
.testTarget(
name: "CommandsTests",
dependencies: ["swift-build", "swift-package", "swift-test", "swift-run", "Commands", "Workspace", "SPMTestSupport"]),
dependencies: ["swift-build", "swift-package", "swift-test", "swift-run", "Commands", "Workspace", "SPMTestSupport", "ArgumentParserTestHelpers"]),
.testTarget(
name: "WorkspaceTests",
dependencies: ["Workspace", "SPMTestSupport"]),
Expand Down
28 changes: 17 additions & 11 deletions Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ extension SwiftPackageTool {
@OptionGroup()
var swiftOptions: SwiftToolOptions

@Option()
@Option(help: "json | text")
var type: DescribeMode = .text

func run(_ swiftTool: SwiftTool) throws {
Expand All @@ -190,22 +190,25 @@ extension SwiftPackageTool {
}
}

struct Init: SwiftCommand {
static let configuration = CommandConfiguration(
public struct Init: SwiftCommand {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need to be public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to use AssertHelp we need access to the commands.

Copy link
Contributor

@tomerd tomerd Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use @testable import to expose internal structures and functions for testing purposes. that is better than exposing more surface area in the public API.

public static let configuration = CommandConfiguration(
abstract: "Initialize a new package")

@OptionGroup()
@OptionGroup(_hiddenFromHelp: true)
var swiftOptions: SwiftToolOptions

@Option(name: .customLong("type"))
@Option(name: .customLong("type"), help: "Package type: empty | library | executable | system-module | manifest")
var initMode: InitPackage.PackageType = .library

@Option(name: .customLong("name"), help: "Provide custom package name")
var packageName: String?


public init() {}

func run(_ swiftTool: SwiftTool) throws {
// FIXME: Error handling.
let cwd = localFileSystem.currentWorkingDirectory!
guard let cwd = localFileSystem.currentWorkingDirectory else {
throw StringError("Could not find the current working directory")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw InternalError in such case, as it would assert in debug mode which is what we want in this failure case

}

let packageName = self.packageName ?? cwd.basename
let initPackage = try InitPackage(
Expand Down Expand Up @@ -343,6 +346,9 @@ extension SwiftPackageTool {
}

struct DumpSymbolGraph: SwiftCommand {
static let configuration = CommandConfiguration(
abstract: "Dump Symbol Graph")

@OptionGroup()
var swiftOptions: SwiftToolOptions

Expand Down Expand Up @@ -472,7 +478,7 @@ extension SwiftPackageTool {
@OptionGroup()
var swiftOptions: SwiftToolOptions

@Option()
@Option(help: "text | dot | json | flatlist")
var format: ShowDependenciesMode = .text

func run(_ swiftTool: SwiftTool) throws {
Expand All @@ -489,7 +495,7 @@ extension SwiftPackageTool {
@OptionGroup()
var swiftOptions: SwiftToolOptions

@Option()
@Option(help: "text | dot | json | flatlist")
var format: ShowDependenciesMode = .text

@Flag(help: "Set tools version of package to the current tools version in use")
Expand Down Expand Up @@ -908,7 +914,7 @@ extension SwiftPackageTool {
@OptionGroup()
var swiftOptions: SwiftToolOptions

@Argument()
@Argument(help: "generate-bash-script | generate-zsh-script |\ngenerate-fish-script | list-dependencies | list-executables")
Copy link
Contributor

@tomerd tomerd Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the argument parser library enumerate the enum options instead of having to explicitly spell them like this? cc @natecook1000

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe with ExpressibleByArgument, I know that it's responsible for filling in default: x

var mode: Mode

func run(_ swiftTool: SwiftTool) throws {
Expand Down
17 changes: 17 additions & 0 deletions Tests/CommandsTests/PackageToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Xcodeproj
import PackageModel
import SourceControl
import SPMTestSupport
import ArgumentParserTestHelpers
import TSCUtility
import Workspace

Expand Down Expand Up @@ -48,6 +49,22 @@ final class PackageToolTests: XCTestCase {
XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout)
}

func testInitHelp() throws {
AssertHelp(for: Commands.SwiftPackageTool.Init.self, equals: """
OVERVIEW: Initialize a new package

USAGE: init [<options>] --enable-index-store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--enable-index-store ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's weird, but it does go away when they're all transitioned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it does go away when they're all transitioned. what does that mean?

Copy link
Contributor

@tomerd tomerd Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another point is that I dont think its scalable to string match the the entire output of the --help for each command. did you intend to add such tests to all commands or just this one as an indicator for the rest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be for all the commands, I think it would be enough to have tests for the most popular commands: e.g. swift run, swift build, swift test

--enable-index-store goes away once all the commands have the option group hidden. In my initial testing that's when I noticed that it would go away.


OPTIONS:
--type <type> Package type: empty | library | executable |
system-module | manifest (default: library)
--name <name> Provide custom package name
-h, --help Show help information.

"""
)
}

func testNetrcSupportedOS() throws {
func verifyUnsupportedOSThrows() {
do {
Expand Down