Skip to content

Update command-line interface for .netrc file handling #3763

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 9 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 18 additions & 13 deletions Sources/Commands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,26 @@ public struct SwiftToolOptions: ParsableArguments {
// Force the Xcode build system if we want to build more than one arch.
archs.count > 1 ? .xcode : _buildSystem
}

/// Whether to load .netrc files for authenticating with remote servers
/// when downloading binary artifacts or communicating with a registry.
@Flag(inversion: .prefixedEnableDisable,
exclusivity: .exclusive,
help: "Load credentials from a .netrc file")
var netrc: Bool = true

/// Tells `Workspace` to attempt to locate .netrc file at `NSHomeDirectory`.
@Flag()
var netrc: Bool = false

/// Similar to `--netrc`, but this option makes the .netrc usage optional and not mandatory as with the `--netrc` option.
@Flag(name: .customLong("netrc-optional"))
var netrcOptional: Bool = false

/// The path to the netrc file which should be use for authentication when downloading binary target artifacts.
/// Similar to `--netrc`, except that you also provide the path to the actual file to use.
/// This is useful when you want to provide the information in another directory or with another file name.
/// - important: Respects `--netrcOptional` option.
@Option(name: .customLong("netrc-file"), completion: .file())
/// The path to the .netrc file used when `netrc` is `true`.
@Option(
name: .customLong("netrc-file"),
help: "Specify the .netrc file path.",
completion: .file())
var netrcFilePath: AbsolutePath?

@Flag(name: .customLong("netrc"), help: .hidden)
var _deprecated_netrc: Bool = false

@Flag(name: .customLong("netrc-optional"), help: .hidden)
var _deprecated_netrcOptional: Bool = false

public init() {}
}
43 changes: 25 additions & 18 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@ public class SwiftTool {
if options.shouldDisableManifestCaching {
diagnostics.emit(warning: "'--disable-package-manifest-caching' option is deprecated; use '--manifest-caching' instead")
}

if let _ = options.netrcFilePath, options.netrc == false {
diagnostics.emit(.mutuallyExclusiveArgumentsError(arguments: ["--disable-netrc", "--netrc-file"]))
}

if options._deprecated_netrc {
diagnostics.emit(warning: "'--netrc' option is deprecated; .netrc files are located by default")
}

if options._deprecated_netrcOptional {
diagnostics.emit(warning: "'--netrc-optional' option is deprecated; .netrc files are located by default")
}
}

private func editsDirectory() throws -> AbsolutePath {
Expand Down Expand Up @@ -485,27 +497,22 @@ public class SwiftTool {
return try self.getNetrcConfig()?.get()
}

func getNetrcConfig() throws -> Workspace.Configuration.Netrc? {
guard options.netrc || options.netrcFilePath != nil || options.netrcOptional else {
return .none
}
func getNetrcConfig() -> Workspace.Configuration.Netrc? {
guard options.netrc else { return nil }

let netrcFilePath = try self.netrcFilePath()
return netrcFilePath.map { .init(path: $0, fileSystem: localFileSystem) }
}

private func netrcFilePath() throws -> AbsolutePath? {
let netrcFilePath = options.netrcFilePath ?? localFileSystem.homeDirectory.appending(component: ".netrc")
guard localFileSystem.exists(netrcFilePath) else {
if !options.netrcOptional {
ObservabilitySystem.topScope.emit(error: "Cannot find mandatory .netrc file at \(netrcFilePath). To make .netrc file optional, use --netrc-optional flag.")
throw ExitCode.failure
} else {
ObservabilitySystem.topScope.emit(warning: "Did not find optional .netrc file at \(netrcFilePath).")
return .none
if let configuredPath = options.netrcFilePath {
guard localFileSystem.exists(configuredPath) else {
ObservabilitySystem.topScope.emit(error: "Did not find .netrc file at \(configuredPath).")
return nil
}

return .init(path: configuredPath, fileSystem: localFileSystem)
} else {
let defaultPath = localFileSystem.homeDirectory.appending(component: ".netrc")
guard localFileSystem.exists(defaultPath) else { return nil }

return .init(path: defaultPath, fileSystem: localFileSystem)
}
return netrcFilePath
}

private func getSharedCacheDirectory() throws -> AbsolutePath? {
Expand Down
79 changes: 48 additions & 31 deletions Tests/CommandsTests/PackageToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ final class PackageToolTests: XCTestCase {
XCTAssertMatch(stdout, .contains("Swift Package Manager"))
}

func testNetrc() throws {
fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in
// --enable-netrc flag
try self.execute(["resolve", "--enable-netrc"], packagePath: packageRoot)

// --disable-netrc flag
try self.execute(["resolve", "--disable-netrc"], packagePath: packageRoot)

// --enable-netrc and --disable-netrc flags
XCTAssertThrowsError(
try self.execute(["resolve", "--enable-netrc", "--disable-netrc"], packagePath: packageRoot)
) { error in
XCTAssertMatch(String(describing: error), .contains("Value to be set with flag '--disable-netrc' had already been set with flag '--enable-netrc'"))
}

// deprecated --netrc flag
let stderr = try self.execute(["resolve", "--netrc"], packagePath: packageRoot).stderr
XCTAssertMatch(stderr, .contains("'--netrc' option is deprecated"))
}
}

func testNetrcOptional() throws {
fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in
let stderr = try self.execute(["resolve", "--netrc-optional"], packagePath: packageRoot).stderr
XCTAssertMatch(stderr, .contains("'--netrc-optional' option is deprecated"))
}
}

func testNetrcFile() throws {
fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in
let fs = localFileSystem
Expand All @@ -58,39 +86,28 @@ final class PackageToolTests: XCTestCase {
stream <<< "machine mymachine.labkey.org login [email protected] password mypassword"
}

do {
// file at correct location
try execute(["--netrc-file", netrcPath.pathString, "resolve"], packagePath: packageRoot)
// file does not exist, but is optional
let textOutput = try execute(["--netrc-file", "/foo", "--netrc-optional", "resolve"], packagePath: packageRoot).stderr
XCTAssertMatch(textOutput, .contains("warning: Did not find optional .netrc file at /foo."))

// required file does not exist, will throw
try execute(["--netrc-file", "/foo", "resolve"], packagePath: packageRoot)
} catch {
XCTAssertMatch(String(describing: error), .contains("Cannot find mandatory .netrc file at /foo"))
// valid .netrc file path
try execute(["resolve", "--netrc-file", netrcPath.pathString], packagePath: packageRoot)

// valid .netrc file path with --disable-netrc option
XCTAssertThrowsError(
try execute(["resolve", "--netrc-file", netrcPath.pathString, "--disable-netrc"], packagePath: packageRoot)
) { error in
XCTAssertMatch(String(describing: error), .contains("'--disable-netrc' and '--netrc-file' are mutually exclusive"))
}
}

fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in
do {
// Developer machine may have .netrc file at NSHomeDirectory; modify test accordingly
if localFileSystem.exists(localFileSystem.homeDirectory.appending(RelativePath(".netrc"))) {
try execute(["--netrc", "resolve"], packagePath: packageRoot)
} else {
// file does not exist, but is optional
let textOutput = try execute(["--netrc", "--netrc-optional", "resolve"], packagePath: packageRoot)
XCTAssertMatch(textOutput.stderr, .contains("Did not find optional .netrc file at \(localFileSystem.homeDirectory)/.netrc."))

// file does not exist, but is optional
let textOutput2 = try execute(["--netrc-optional", "resolve"], packagePath: packageRoot)
XCTAssertMatch(textOutput2.stderr, .contains("Did not find optional .netrc file at \(localFileSystem.homeDirectory)/.netrc."))

// required file does not exist, will throw
try execute(["--netrc", "resolve"], packagePath: packageRoot)
}
} catch {
XCTAssertMatch(String(describing: error), .contains("Cannot find mandatory .netrc file at \(localFileSystem.homeDirectory)/.netrc"))
// invalid .netrc file path
XCTAssertThrowsError(
try execute(["resolve", "--netrc-file", "/foo"], packagePath: packageRoot)
) { error in
XCTAssertMatch(String(describing: error), .contains("Did not find .netrc file at /foo."))
}

// invalid .netrc file path with --disable-netrc option
XCTAssertThrowsError(
try execute(["resolve", "--netrc-file", "/foo", "--disable-netrc"], packagePath: packageRoot)
) { error in
XCTAssertMatch(String(describing: error), .contains("'--disable-netrc' and '--netrc-file' are mutually exclusive"))
}
}
}
Expand Down