Skip to content
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

Merge duplicated imports in finally-generated Dangerfile #631

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DangerfileExtensions/ChangelogCheck.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Danger

func checkChangelog() {
let allSourceFiles = danger.git.modifiedFiles + danger.git.createdFiles

Expand Down
19 changes: 19 additions & 0 deletions Sources/RunnerLib/Files Import/DangerFileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public final class DangerFileGenerator {
dangerContent.replaceSubrange(replacementRange, with: fileContent)
})

mergeImports(in: &dangerContent)

try dangerContent.write(toFile: fileName, atomically: false, encoding: .utf8)
}
}

private extension DangerFileGenerator {
func mergeImports(in content: inout String) {
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")
}
}
50 changes: 46 additions & 4 deletions Tests/RunnerLibTests/DangerFileGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ final class DangerFileGeneratorTests: XCTestCase {
}

func testItGeneratesTheCorrectFileWhenThereAreNoImports() throws {
try generator.generateDangerFile(fromContent: contentWithoutImports, fileName: generatedFilePath, logger: logger)
try generator.generateDangerFile(
fromContent: headerForContentWithoutImports + contentWithoutImports,
fileName: generatedFilePath,
logger: logger
)

try assertSnapshot(matching: generatedContent(), as: .lines)
}
Expand Down Expand Up @@ -83,9 +87,29 @@ 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 {
private var headerForContentWithoutImports: String {
"""
import Danger

let danger = Danger()
""" + "\n\n"
}

private var contentWithoutImports: String {
"""
message("Text")
Expand All @@ -94,12 +118,25 @@ extension DangerFileGeneratorTests {
}

private var contentWithOneImport: String {
"// fileImport: " + file1Path + "\n" + contentWithoutImports
headerForContentWithoutImports
+ "// fileImport: " + file1Path + "\n"
+ contentWithoutImports
}

private var contentWithMultipleImports: String {
"// fileImport: " + file2Path + "\n\n" +
"// fileImport: " + file3Path + "\n" + contentWithOneImport
"// fileImport: " + file2Path + "\n\n"
+ "// fileImport: " + file3Path + "\n"
+ contentWithOneImport
}

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

private var file1Content: String {
Expand All @@ -111,12 +148,17 @@ extension DangerFileGeneratorTests {

private var file2Content: String {
"""
import Danger

file2Content ⚠️
"""
}

private var file3Content: String {
"""
import Danger
import Foundation

file3Content 👩‍👩‍👦‍👦
secondLine
really really really really really really really really really really really really \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Danger

file2Content ⚠️

// fileImport: GeneratedTestFile3.swift

let danger = Danger()

file1Content 👍🏻
secondLine
message("Text")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import Danger

let danger = Danger()

message("Text")
message("Another Text")
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Danger

let danger = Danger()

file1Content 👍🏻
secondLine
message("Text")
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")
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Danger
import Foundation

file2Content ⚠️


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

let danger = Danger()

file1Content 👍🏻
secondLine
message("Text")
Expand Down
Loading