Skip to content

Commit

Permalink
add fileImport with indent pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
417-72KI committed Nov 11, 2024
1 parent f88b659 commit 702b171
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Sources/RunnerLib/Files Import/DangerFileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ public final class DangerFileGenerator {

private extension DangerFileGenerator {
func mergeImports(in content: inout String) {
var lines = content.split(separator: "\n",
omittingEmptySubsequences: false)
let imports = Set(lines.filter { $0.hasPrefix("import ") })
lines.removeAll { imports.contains($0) }
var lines = content
.split(separator: "\n",
omittingEmptySubsequences: false)
.map(String.init)

let imports = Set(
lines.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { $0.hasPrefix("import ") }
)
lines.removeAll { imports.contains($0.trimmingCharacters(in: .whitespaces)) }
lines.insert(contentsOf: imports.sorted(), at: 0)
content = lines.joined(separator: "\n")
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/RunnerLibTests/DangerFileGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ final class DangerFileGeneratorTests: XCTestCase {

try assertSnapshot(matching: generatedContent(), as: .lines)
}

func testItGeneratesTheCorrectFileWhenThereIsAreImportsWithIndent() throws {
try? file2Content.write(toFile: file2Path, atomically: true, encoding: .utf8)
try? file3Content.write(toFile: file3Path, atomically: true, encoding: .utf8)

createdFiles.append(file2Path)
createdFiles.append(file3Path)

try generator.generateDangerFile(fromContent: contentWithImportsWithIndent, fileName: generatedFilePath, logger: logger)

try assertSnapshot(matching: generatedContent(), as: .lines)
}
}

extension DangerFileGeneratorTests {
Expand Down Expand Up @@ -117,6 +129,16 @@ extension DangerFileGeneratorTests {
+ contentWithOneImport
}

private var contentWithImportsWithIndent: String {
headerForContentWithoutImports
+ "if flag {\n"
+ " // fileImport: " + file2Path + "\n"
+ "} else {\n"
+ " // fileImport: " + file3Path + "\n"
+ "}\n"
+ contentWithoutImports
}

private var file1Content: String {
"""
file1Content 👍🏻
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Danger
import Foundation

let danger = Danger()

if flag {

file2Content ⚠️
} else {

file3Content 👩‍👩‍👦‍👦
secondLine
really really really really really really really really really really really really really really really really really really really really really really long text
}
message("Text")
message("Another Text")

0 comments on commit 702b171

Please sign in to comment.