Skip to content

Commit 6fa922a

Browse files
ahoppenmacshome
authored andcommitted
Run Windows tests dockerless (+13 squashed commits)
Squashed commits: [23709e8] Fix issue in publish_release pipeline testing swift-format in debug configuration [ce212ca] Use matrix to run debug and release [39ee6a2] Run Windows tests before tagging a release The GitHub workflows enabled Windows in the testing matrix. We needed to port the pre-build commands that apply the release commits to Windows to make the Windows checks pass. [8fec655] Use dockerless Windows jobs Dockerless Windows is 5-10 minutes faster than Docker on Windows [2cd032c] PrettyPrinter reports wrong line LineNumbersTests The reason for the wrong line number were multiline comments. In to accomodate for this, we now check the string while writing for new lines and increment the line count accordingly. Issue: #882 [8c68ec3] Add indentBlankLines configuration [fee42c9] Add `--enable-experimental-feature` to enable those features in the parser. Also add a couple small tests for value generics to exercise the capability in tests. Fixes #875. [5e4caa8] feat: add pre-commit hooks [e6aa9ec] Update UseShorthandTypeNames.swift [9cbc942] Update UseShorthandTypeNames.swift [dfb366a] Fix formatting [c3b0c9f] Prepare for integer generics with new generic argument node [211884f] Fix tests when building swift-format using Swift 6.0.2 When traversing the file URL with Foundation from Swift 6.0.2, you get the following components - `["/", "C:", "test.swift"]` - `["/", "C:"]` - `[]` The component count never reaches 1. Foundation from Swift 6.1 goes - `["/", "C:", "test.swift"]` - `["/", "C:"]` - `["/"]` Cover both cases by checking for `<= 1` instead of `== 1` Prepare for integer generics with new generic argument node Fix formatting Update UseShorthandTypeNames.swift Update UseShorthandTypeNames.swift feat: add pre-commit hooks Add `--enable-experimental-feature` to enable those features in the parser. Also add a couple small tests for value generics to exercise the capability in tests. Fixes #875. Add indentBlankLines configuration PrettyPrinter reports wrong line LineNumbersTests The reason for the wrong line number were multiline comments. In to accomodate for this, we now check the string while writing for new lines and increment the line count accordingly. Issue: #882 Use dockerless Windows jobs Dockerless Windows is 5-10 minutes faster than Docker on Windows Run Windows tests before tagging a release The GitHub workflows enabled Windows in the testing matrix. We needed to port the pre-build commands that apply the release commits to Windows to make the Windows checks pass. Use matrix to run debug and release Fix issue in publish_release pipeline testing swift-format in debug configuration Run Windows tests dockerless
1 parent 90931a6 commit 6fa922a

File tree

6 files changed

+181
-52
lines changed

6 files changed

+181
-52
lines changed

.github/workflows/create-release-commits.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

.github/workflows/publish_release.yml

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ jobs:
3434
echo "${{ github.triggering_actor }} is not allowed to create a release"
3535
exit 1
3636
fi
37-
define_tags:
38-
name: Determine dependent swift-syntax version and prerelease date
37+
create_release_commits:
38+
name: Create release commits
3939
runs-on: ubuntu-latest
4040
outputs:
41-
swift_syntax_tag: ${{ steps.swift_syntax_tag.outputs.swift_syntax_tag }}
4241
swift_format_version: ${{ steps.swift_format_version.outputs.swift_format_version }}
42+
release_commit_patch: ${{ steps.create_release_commits.outputs.release_commit_patch }}
4343
steps:
4444
- name: Determine swift-syntax tag to depend on
4545
id: swift_syntax_tag
@@ -65,37 +65,81 @@ jobs:
6565
fi
6666
echo "Using swift-format version: $SWIFT_FORMAT_VERSION"
6767
echo "swift_format_version=$SWIFT_FORMAT_VERSION" >> "$GITHUB_OUTPUT"
68-
test_debug:
69-
name: Test in Debug configuration
70-
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
71-
needs: define_tags
72-
with:
73-
pre_build_command: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
74-
# We require that releases of swift-format build without warnings
75-
build_command: swift test -Xswiftc -warnings-as-errors
76-
test_release:
77-
name: Test in Release configuration
68+
- name: Checkout repository
69+
uses: actions/checkout@v4
70+
- name: Create release commits
71+
id: create_release_commits
72+
run: |
73+
# Without this, we can't perform git operations in GitHub actions.
74+
git config --global --add safe.directory "$(realpath .)"
75+
git config --local user.name 'swift-ci'
76+
git config --local user.email '[email protected]'
77+
78+
BASE_COMMIT=$(git rev-parse HEAD)
79+
80+
sed -E -i "s#branch: \"(main|release/[0-9]+\.[0-9]+)\"#from: \"${{ steps.swift_syntax_tag.outputs.swift_syntax_tag }}\"#" Package.swift
81+
git add Package.swift
82+
git commit -m "Change swift-syntax dependency to ${{ steps.swift_syntax_tag.outputs.swift_syntax_tag }}"
83+
84+
sed -E -i "s#print\(\".*\"\)#print\(\"${{ steps.swift_format_version.outputs.swift_format_version }}\"\)#" Sources/swift-format/PrintVersion.swift
85+
git add Sources/swift-format/PrintVersion.swift
86+
git commit -m "Change version to ${{ steps.swift_format_version.outputs.swift_format_version }}"
87+
88+
{
89+
echo 'release_commit_patch<<EOF'
90+
git format-patch "$BASE_COMMIT"..HEAD --stdout
91+
echo EOF
92+
} >> "$GITHUB_OUTPUT"
93+
test:
94+
name: Test in ${{ matrix.release && 'Release' || 'Debug' }} configuration
7895
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
79-
needs: define_tags
96+
needs: create_release_commits
97+
strategy:
98+
fail-fast: false
99+
matrix:
100+
release: [true, false]
80101
with:
81-
pre_build_command: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
102+
enable_windows_docker: false # Dockerless Windows is 5-10 minutes faster than Docker on Windows
103+
linux_pre_build_command: |
104+
git config --global --add safe.directory "$(realpath .)"
105+
git config --local user.name 'swift-ci'
106+
git config --local user.email '[email protected]'
107+
git am << EOF
108+
${{ needs.create_release_commits.outputs.release_commit_patch }}
109+
EOF
110+
windows_pre_build_command: |
111+
git config --local user.name "swift-ci"
112+
git config --local user.email "[email protected]"
113+
echo @"
114+
${{ needs.create_release_commits.outputs.release_commit_patch }}
115+
"@ > $env:TEMP\patch.diff
116+
# For some reason `git am` fails in Powershell with the following error. Executing it in cmd works...
117+
# fatal: empty ident name (for <>) not allowed
118+
cmd /c "type $env:TEMP\patch.diff | git am || (exit /b 1)"
82119
# We require that releases of swift-format build without warnings
83-
build_command: swift test -c release -Xswiftc -warnings-as-errors
120+
linux_build_command: swift test -Xswiftc -warnings-as-errors ${{ matrix.release && '-c release' || '' }}
121+
windows_build_command: swift test -Xswiftc -warnings-as-errors ${{ matrix.release && '-c release' || '' }}
84122
create_tag:
85123
name: Create Tag
86124
runs-on: ubuntu-latest
87-
needs: [check_triggering_actor, test_debug, test_release, define_tags]
125+
needs: [check_triggering_actor, test, create_release_commits]
88126
permissions:
89127
contents: write
90128
steps:
91129
- name: Checkout repository
92130
uses: actions/checkout@v4
93-
- name: Create release commits
94-
run: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
131+
- name: Apply release commits
132+
run: |
133+
git config --global --add safe.directory "$(realpath .)"
134+
git config --local user.name 'swift-ci'
135+
git config --local user.email '[email protected]'
136+
git am << EOF
137+
${{ needs.create_release_commits.outputs.release_commit_patch }}
138+
EOF
95139
- name: Tag release
96140
run: |
97-
git tag "${{ needs.define_tags.outputs.swift_format_version }}"
98-
git push origin "${{ needs.define_tags.outputs.swift_format_version }}"
141+
git tag "${{ needs.create_release_commits.outputs.swift_format_version }}"
142+
git push origin "${{ needs.create_release_commits.outputs.swift_format_version }}"
99143
- name: Create release
100144
env:
101145
GH_TOKEN: ${{ github.token }}
@@ -104,6 +148,6 @@ jobs:
104148
# Only create a release automatically for prereleases. For real releases, release notes should be crafted by hand.
105149
exit
106150
fi
107-
gh release create "${{ needs.define_tags.outputs.swift_format_version }}" \
108-
--title "${{ needs.define_tags.outputs.swift_format_version }}" \
151+
gh release create "${{ needs.create_release_commits.outputs.swift_format_version }}" \
152+
--title "${{ needs.create_release_commits.outputs.swift_format_version }}" \
109153
--prerelease

.github/workflows/pull_request.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ jobs:
88
tests:
99
name: Test
1010
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
11+
with:
12+
enable_windows_docker: false # Dockerless Windows is 5-10 minutes faster than Docker on Windows
1113
soundness:
1214
name: Soundness
1315
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,17 @@ public struct NoAssignmentInExpressionsConfiguration: Codable, Equatable {
486486

487487
public init() {}
488488
}
489+
490+
fileprivate extension URL {
491+
var isRoot: Bool {
492+
#if os(Windows)
493+
// FIXME: We should call into Windows' native check to check if this path is a root once https://github.com/swiftlang/swift-foundation/issues/976 is fixed.
494+
// https://github.com/swiftlang/swift-format/issues/844
495+
return self.pathComponents.count <= 1
496+
#else
497+
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
498+
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
499+
return self.path == "/" || self.path == ""
500+
#endif
501+
}
502+
}

Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,20 @@ struct PrettyPrintBuffer {
118118
writeRaw(text)
119119
consecutiveNewlineCount = 0
120120
pendingSpaces = 0
121-
column += text.count
121+
122+
// In case of comments, we may get a multi-line string.
123+
// To account for that case, we need to correct the lineNumber count.
124+
// The new column is only the position within the last line.
125+
let lines = text.split(separator: "\n")
126+
lineNumber += lines.count - 1
127+
if lines.count > 1 {
128+
// in case we have inserted new lines, we need to reset the column
129+
column = lines.last?.count ?? 0
130+
} else {
131+
// in case it is an end of line comment or a single line comment,
132+
// we just add to the current column
133+
column += lines.last?.count ?? 0
134+
}
122135
}
123136

124137
/// Request that the given number of spaces be printed out before the next text token.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import SwiftFormat
2+
import _SwiftFormatTestSupport
3+
4+
final class LineNumbersTests: PrettyPrintTestCase {
5+
func testLineNumbers() {
6+
let input =
7+
"""
8+
final class A {
9+
@Test func b() throws {
10+
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
11+
}
12+
}
13+
"""
14+
15+
let expected =
16+
"""
17+
final class A {
18+
@Test func b() throws {
19+
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
20+
}
21+
}
22+
23+
"""
24+
25+
assertPrettyPrintEqual(
26+
input: input,
27+
expected: expected,
28+
linelength: 120,
29+
whitespaceOnly: true,
30+
findings: [
31+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
32+
]
33+
)
34+
}
35+
36+
func testLineNumbersWithComments() {
37+
let input =
38+
"""
39+
// Copyright (C) 2024 My Coorp. All rights reserved.
40+
//
41+
// This document is the property of My Coorp.
42+
// It is considered confidential and proprietary.
43+
//
44+
// This document may not be reproduced or transmitted in any form,
45+
// in whole or in part, without the express written permission of
46+
// My Coorp.
47+
48+
final class A {
49+
@Test func b() throws {
50+
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
51+
}
52+
}
53+
"""
54+
55+
let expected =
56+
"""
57+
// Copyright (C) 2024 My Coorp. All rights reserved.
58+
//
59+
// This document is the property of My Coorp.
60+
// It is considered confidential and proprietary.
61+
//
62+
// This document may not be reproduced or transmitted in any form,
63+
// in whole or in part, without the express written permission of
64+
// My Coorp.
65+
66+
final class A {
67+
@Test func b() throws {
68+
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
69+
}
70+
}
71+
72+
"""
73+
74+
assertPrettyPrintEqual(
75+
input: input,
76+
expected: expected,
77+
linelength: 120,
78+
whitespaceOnly: true,
79+
findings: [
80+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
81+
]
82+
)
83+
}
84+
}

0 commit comments

Comments
 (0)