Skip to content

Commit 702b171

Browse files
committed
add fileImport with indent pattern
1 parent f88b659 commit 702b171

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

Sources/RunnerLib/Files Import/DangerFileGenerator.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ public final class DangerFileGenerator {
3333

3434
private extension DangerFileGenerator {
3535
func mergeImports(in content: inout String) {
36-
var lines = content.split(separator: "\n",
37-
omittingEmptySubsequences: false)
38-
let imports = Set(lines.filter { $0.hasPrefix("import ") })
39-
lines.removeAll { imports.contains($0) }
36+
var lines = content
37+
.split(separator: "\n",
38+
omittingEmptySubsequences: false)
39+
.map(String.init)
40+
41+
let imports = Set(
42+
lines.map { $0.trimmingCharacters(in: .whitespaces) }
43+
.filter { $0.hasPrefix("import ") }
44+
)
45+
lines.removeAll { imports.contains($0.trimmingCharacters(in: .whitespaces)) }
4046
lines.insert(contentsOf: imports.sorted(), at: 0)
4147
content = lines.joined(separator: "\n")
4248
}

Tests/RunnerLibTests/DangerFileGeneratorTests.swift

+22
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ final class DangerFileGeneratorTests: XCTestCase {
8787

8888
try assertSnapshot(matching: generatedContent(), as: .lines)
8989
}
90+
91+
func testItGeneratesTheCorrectFileWhenThereIsAreImportsWithIndent() throws {
92+
try? file2Content.write(toFile: file2Path, atomically: true, encoding: .utf8)
93+
try? file3Content.write(toFile: file3Path, atomically: true, encoding: .utf8)
94+
95+
createdFiles.append(file2Path)
96+
createdFiles.append(file3Path)
97+
98+
try generator.generateDangerFile(fromContent: contentWithImportsWithIndent, fileName: generatedFilePath, logger: logger)
99+
100+
try assertSnapshot(matching: generatedContent(), as: .lines)
101+
}
90102
}
91103

92104
extension DangerFileGeneratorTests {
@@ -117,6 +129,16 @@ extension DangerFileGeneratorTests {
117129
+ contentWithOneImport
118130
}
119131

132+
private var contentWithImportsWithIndent: String {
133+
headerForContentWithoutImports
134+
+ "if flag {\n"
135+
+ " // fileImport: " + file2Path + "\n"
136+
+ "} else {\n"
137+
+ " // fileImport: " + file3Path + "\n"
138+
+ "}\n"
139+
+ contentWithoutImports
140+
}
141+
120142
private var file1Content: String {
121143
"""
122144
file1Content 👍🏻
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Danger
2+
import Foundation
3+
4+
let danger = Danger()
5+
6+
if flag {
7+
8+
file2Content ⚠️
9+
} else {
10+
11+
file3Content 👩‍👩‍👦‍👦
12+
secondLine
13+
really really really really really really really really really really really really really really really really really really really really really really long text
14+
}
15+
message("Text")
16+
message("Another Text")

0 commit comments

Comments
 (0)