diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 9b15ee1..a69e681 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -2,17 +2,34 @@ name: Swift on: push: + branches: [ "main" ] tags: - "**" + pull_request: + branches: [ "main" ] permissions: contents: write jobs: - build: + test: runs-on: macos-latest steps: - uses: actions/checkout@v4 + - uses: swift-actions/setup-swift@v3 + with: + swift-version: "6.4.2" + - name: Run tests + run: swift test -v + + release: + if: startsWith(github.ref, 'refs/tags/') + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: swift-actions/setup-swift@v3 + with: + swift-version: "6.4.2" - name: Build run: swift build -v --disable-sandbox -c release --arch arm64 --arch x86_64 - name: Release diff --git a/Package.resolved b/Package.resolved index 4c73192..3b9c44d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,12 +1,13 @@ { + "originHash" : "843dd0852fc4684234ee69724c1aef81422b9e7b857e2a9fe4467651bcab0921", "pins" : [ { "identity" : "swift-argument-parser", "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-argument-parser.git", "state" : { - "revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b", - "version" : "1.4.0" + "revision" : "6a52f3251125d74daf04fcbd5e6f08a75d074382", + "version" : "1.8.2" } }, { @@ -14,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/swiftlang/swift-cmark.git", "state" : { - "branch" : "gfm", - "revision" : "2c47322cb32cbed555f13bf5cbfaa488cc30a785" + "revision" : "08ddb528923cc1a6527e02b7a1aee9e516ca749a", + "version" : "0.9.0" } }, { @@ -23,10 +24,10 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-markdown.git", "state" : { - "branch" : "main", - "revision" : "d21714073e0d16ba78eebdf36724863afc36871d" + "revision" : "25cb61d3482054b09ae76ca4f281b1bfe7fe5a43", + "version" : "0.9.0" } } ], - "version" : 2 + "version" : 3 } diff --git a/Package.swift b/Package.swift index bc4bfc3..ce3ce71 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9.0 +// swift-tools-version: 6.4 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -7,8 +7,8 @@ let package = Package( name: "swift-changelog-parser", platforms: [.macOS(.v13)], dependencies: [ - .package(url: "https://github.com/apple/swift-markdown.git", branch: "main"), - .package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.4.0")), + .package(url: "https://github.com/apple/swift-markdown.git", .upToNextMajor(from: "0.9.0")), + .package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.8.2")), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. @@ -16,6 +16,22 @@ let package = Package( .executableTarget( name: "swift-changelog-parser", dependencies: [.product(name: "Markdown", package: "swift-markdown"), - .product(name: "ArgumentParser", package: "swift-argument-parser")]), + .product(name: "ArgumentParser", package: "swift-argument-parser")], + swiftSettings: swiftSettings), + .testTarget( + name: "swift-changelog-parserTests", + dependencies: ["swift-changelog-parser"], + swiftSettings: swiftSettings), ] ) + +var swiftSettings: [SwiftSetting] { + [ + .swiftLanguageMode(.v6), + .enableUpcomingFeature("ExistentialAny"), + .enableUpcomingFeature("InternalImportsByDefault"), + .enableUpcomingFeature("MemberImportVisibility"), + .enableUpcomingFeature("InferIsolatedConformances"), + .enableUpcomingFeature("ImmutableWeakCaptures"), + ] +} diff --git a/Sources/Changelog.swift b/Sources/Changelog.swift index a453a55..ced5e1f 100644 --- a/Sources/Changelog.swift +++ b/Sources/Changelog.swift @@ -71,6 +71,16 @@ struct Changelog: ParsableCommand { func run() throws { let document = try Document(parsing: URL(filePath: path)) + let section = try Changelog.section(of: document, for: release) + + if section.isEmpty { + Logger.changelog.info("Changelog does not contain elements in the '\(release.description.localizedCapitalized, privacy: .public)' section") + } + + print(section) + } + + static func section(of document: Document, for release: Release) throws -> String { let heading = document.children .compactMap { $0 as? Heading } .filter { $0.level == 2 } @@ -82,11 +92,9 @@ struct Changelog: ParsableCommand { let list = document.child(at: heading.indexInParent + 1) guard let list, list is UnorderedList else { - Logger.changelog.info("Changelog does not contain elements in the '\(release.description.localizedCapitalized, privacy: .public)' section") - print("") - return + return "" } - print(list.format().trimmingCharacters(in: .whitespacesAndNewlines)) + return list.format().trimmingCharacters(in: .whitespacesAndNewlines) } } diff --git a/Tests/swift-changelog-parserTests/ChangelogTests.swift b/Tests/swift-changelog-parserTests/ChangelogTests.swift new file mode 100644 index 0000000..28919de --- /dev/null +++ b/Tests/swift-changelog-parserTests/ChangelogTests.swift @@ -0,0 +1,57 @@ +import ArgumentParser +import Markdown +import Testing +@testable import swift_changelog_parser + +let changelog = """ +# Changelog + +## Unreleased + +- Added thing +- Fixed thing + +## 1.1.0 + +- Second release + +## 1.0.0 + +- First release +""" + +@Suite struct ChangelogTests { + + func section(_ release: Changelog.Release, markdown: String = changelog) throws -> String { + try Changelog.section(of: Document(parsing: markdown), for: release) + } + + @Test func unreleased() throws { + #expect(try section(.unreleased) == "- Added thing\n- Fixed thing") + } + + @Test func latest() throws { + #expect(try section(.latest) == "- Second release") + } + + @Test func specificRelease() throws { + #expect(try section(.release("1.0.0")) == "- First release") + } + + @Test func missingSectionThrows() { + #expect(throws: ValidationError.self) { + try section(.release("2.0.0")) + } + } + + @Test func emptySectionReturnsEmptyString() throws { + let markdown = """ + # Changelog + + ## Unreleased + + Some prose, but no list. + """ + #expect(try section(.unreleased, markdown: markdown).isEmpty) + } +}