-
Notifications
You must be signed in to change notification settings - Fork 51
Refactor list command to support json format #388
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
Open
roulpriya
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
roulpriya:refactor-list-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -46,15 +46,16 @@ import Testing | |
} | ||
|
||
let output = try await SwiftlyTests.runWithMockedIO(List.self, args) | ||
let lines = output.flatMap { $0.split(separator: "\n").map(String.init) } | ||
|
||
let parsedToolchains = output.compactMap { outputLine in | ||
let parsedToolchains = lines.compactMap { outputLine in | ||
Set<ToolchainVersion>.allToolchains().first { | ||
outputLine.contains(String(describing: $0)) | ||
} | ||
} | ||
|
||
// Ensure extra toolchains weren't accidentally included in the output. | ||
guard parsedToolchains.count == output.filter({ $0.hasPrefix("Swift") || $0.contains("-snapshot") }).count else { | ||
guard parsedToolchains.count == lines.filter({ $0.hasPrefix("Swift") || $0.contains("-snapshot") }).count else { | ||
throw SwiftlyTestError(message: "unexpected listed toolchains in \(output)") | ||
} | ||
|
||
|
@@ -127,8 +128,9 @@ import Testing | |
} | ||
|
||
let output = try await SwiftlyTests.runWithMockedIO(List.self, listArgs) | ||
let lines = output.flatMap { $0.split(separator: "\n").map(String.init) } | ||
|
||
let inUse = output.filter { $0.contains("in use") } | ||
let inUse = lines.filter { $0.contains("in use") && $0.contains(toolchain.name) } | ||
#expect(inUse == ["\(toolchain) (in use) (default)"]) | ||
} | ||
|
||
|
@@ -173,4 +175,94 @@ import Testing | |
#expect(toolchains == []) | ||
} | ||
} | ||
|
||
/// Tests that running `list` command with JSON format outputs correctly structured JSON. | ||
@Test func listJsonFormat() async throws { | ||
try await self.runListTest { | ||
let output = try await SwiftlyTests.runWithMockedIO( | ||
List.self, ["list", "--format", "json"], format: .json | ||
) | ||
|
||
let result = try JSONSerialization.jsonObject( | ||
with: output[0].data(using: .utf8)!, options: [] | ||
) as! [String: Any] | ||
|
||
#expect(result["toolchains"] != nil) | ||
let toolchains = result["toolchains"] as! [[String: Any]] | ||
#expect(toolchains.count == Set<ToolchainVersion>.allToolchains().count) | ||
|
||
for toolchain in toolchains { | ||
#expect(toolchain["version"] != nil) | ||
#expect(toolchain["inUse"] != nil) | ||
#expect(toolchain["default"] != nil) | ||
|
||
let version = toolchain["version"] as! [String: Any] | ||
#expect(version["name"] != nil) | ||
#expect(version["type"] != nil) | ||
} | ||
} | ||
} | ||
|
||
/// Tests that running `list` command with JSON format and selector outputs filtered results. | ||
@Test func listJsonFormatWithSelector() async throws { | ||
try await self.runListTest { | ||
var output = try await SwiftlyTests.runWithMockedIO( | ||
List.self, ["list", "5", "--format", "json"], format: .json | ||
) | ||
|
||
var result = try JSONSerialization.jsonObject( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: same as above. Why not decode into a |
||
with: output[0].data(using: .utf8)!, options: [] | ||
) as! [String: Any] | ||
|
||
var toolchains = result["toolchains"] as! [[String: Any]] | ||
#expect(toolchains.count == Self.sortedReleaseToolchains.count) | ||
|
||
for toolchain in toolchains { | ||
let version = toolchain["version"] as! [String: Any] | ||
#expect(version["type"] as! String == "stable") | ||
} | ||
|
||
output = try await SwiftlyTests.runWithMockedIO( | ||
List.self, ["list", "main-snapshot", "--format", "json"], format: .json | ||
) | ||
|
||
result = try JSONSerialization.jsonObject( | ||
with: output[0].data(using: .utf8)!, options: [] | ||
) as! [String: Any] | ||
|
||
toolchains = result["toolchains"] as! [[String: Any]] | ||
#expect(toolchains.count == 2) | ||
|
||
for toolchain in toolchains { | ||
let version = toolchain["version"] as! [String: Any] | ||
#expect(version["type"] as! String == "snapshot") | ||
#expect(version["branch"] as! String == "main") | ||
} | ||
} | ||
} | ||
|
||
/// Tests that the JSON output correctly indicates which toolchain is in use. | ||
@Test func listJsonFormatInUse() async throws { | ||
try await self.runListTest { | ||
try await SwiftlyTests.runCommand(Use.self, ["use", ToolchainVersion.newStable.name]) | ||
|
||
let output = try await SwiftlyTests.runWithMockedIO( | ||
List.self, ["list", "--format", "json"], format: .json | ||
) | ||
|
||
let result = try JSONSerialization.jsonObject( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: see above |
||
with: output[0].data(using: .utf8)!, options: [] | ||
) as! [String: Any] | ||
|
||
let toolchains = result["toolchains"] as! [[String: Any]] | ||
|
||
let inUseToolchains = toolchains.filter { $0["inUse"] as! Bool } | ||
#expect(inUseToolchains.count == 1) | ||
|
||
let inUseToolchain = inUseToolchains[0] | ||
let version = inUseToolchain["version"] as! [String: Any] | ||
#expect(version["name"] as! String == ToolchainVersion.newStable.name) | ||
#expect(inUseToolchain["default"] as! Bool == true) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Instead of decoding this JSON data into a dictionary, why not decode into a
InstalledToolchainsListInfo
instead? The checks below would just be simple field accesses instead of dictionary lookups.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing that forces us to implement
Decodable
in all the structs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be much effort to make them Decodable?