-
Notifications
You must be signed in to change notification settings - Fork 14
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
Handle reporters with absolute path as output path #49
Conversation
Current coverage is 93.10% (diff: 100%)@@ master #49 diff @@
==========================================
Files 61 61
Lines 1657 1683 +26
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 1537 1567 +30
+ Misses 120 116 -4
Partials 0 0
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mentioned issue itself is fixed.
@@ -33,14 +33,14 @@ func !=(lhs: [String:AnyObject], rhs: [String:AnyObject]) -> Bool { | |||
|
|||
class ReporterComparator { | |||
|
|||
func compareReporters(_ firstReporterPath: String, secondReporterPath: String) -> Bool { | |||
var violations1 = JSONToViolationParser().parseFile(firstReporterPath) | |||
func compareReporter(atPath path1: String, withReporterAtPath path2: String) -> Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the function that gets two paths, then parses this files to get violations, then compares each violation through iteration.
All this is happening in compareReporter(atPath:withReporterAtPath:)
function of ReporterComparator
class.
The Reporter word leads to a delusion here(we also have reporters in this project).
Can you think about a better name, like compareViolations
, or for a better place/implementation of this functionality(extension)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class takes 2 paths to reports and compare them. I'm not interested how it compares the reports, it's his problem. Also, If I would see compareViolations
function I would think that I should give it an array of Violation
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, maybe will be better to name it compareReport(atPath:withReportAtPath:)
(report and not reporter)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already done that.
} | ||
} | ||
|
||
private func writeOutput(with violations: [Violation], reporter: Reporter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can get rid of the Output
word here.
Also, there is a Typo in commit: |
@mihai8804858 Tests are failing 😢 |
@mihai8804858 @lexorus Checks are failing because code coverage decreased. 😢 |
private func addElements(to xml: XMLDocument, from violations: [Violation]) { | ||
for path in filePaths(from: violations) { | ||
guard let fileElement = xmlElement(withFilePath: path) else { continue } | ||
violations.filter { $0.path == path } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we do a lot of iteration here. A big number of violations can lead to a computation time growth.
One way we can reduce the numbers of loops is by iterating through violations (just once), group them into a dictionary that uses paths as keys:
private typealias ViolationsMap = [Path: [Violation]]
final class PMDCoordinator: WritingCoordinator {
func writeViolations(_ violations: [Violation], atPath path: String) {
FileManager().removeFileAtPath(path)
let map = mapViolations(violations)
let xml = generateXML(from: map)
let xmlData = xml.xmlData(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue))
do {
try xmlData.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch let error {
let message = "Error while writing XML object at path: " + path + "\n" +
"Reason: " + error.localizedDescription
Printer(verbosityLevel: .error).printError(message)
}
}
private func mapViolations(_ violations: [Violation]) -> ViolationsMap {
return violations.reduce([Path : [Violation]]()) { result, violation in
let path = violation.path
// Add violation to the group with the same path
var violations = result[path] ?? []
violations.append(violation)
// Update the result
var nextResult = result
nextResult[path] = violations
return result
}
}
private func generateXML(from violationsMap: ViolationsMap) -> XMLDocument {
let xml = XMLDocument(rootElement: XMLElement(name: "pmd"))
xml.version = "1.0"
xml.characterEncoding = "UTF-8"
let fileElements = violationsMap.map(generateFileElement)
xml.rootElement()?.addChildren(fileElements)
return xml
}
private func generateFileElement(path: Path, violations: [Violation]) -> XMLElement {
let element = XMLElement(name: "file")
let attribute = XMLNode(kind: .attribute)
attribute.setValue(path, forKey: "name")
element.addAttribute(attribute)
violations.forEach { element.addChild($0.toXMLElement()) }
return element
}
}
extension XMLElement {
func addChildren(_ children: [XMLElement]) {
children.forEach { child in
addChild(child)
}
}
}
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you did a good job! 👍
@@ -17,7 +17,7 @@ enum IntegrationTestsError: Error { | |||
|
|||
class TaylorIntegrationTests: QuickSpec { | |||
|
|||
var runTaskPath : String { | |||
var taylorExecutablePath : String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about executableFilePath
?
If we rename the tool, we don't want to be needed to make too many changes.
@@ -88,6 +88,23 @@ class TaylorIntegrationTests: QuickSpec { | |||
} | |||
} | |||
|
|||
fileprivate func runTaylorWithArguments(_ arguments: String...) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runWithArguments
?
} | ||
} catch let error { | ||
print(error) | ||
context("when specifie report with relative path") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo here at specifie
} | ||
} | ||
|
||
context("when specifie report with absolute path") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same tipo (specifie
) here 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo here: tipo
-> typo
😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣
@mihai8804858 There is a typo in the commit message. Could you please fix it? |
@@ -26,6 +36,18 @@ class NSFileManagerTests: QuickSpec { | |||
manager.removeFileAtPath(filePath) | |||
expect(manager.fileExists(atPath: filePath)).to(beFalse()) | |||
} | |||
|
|||
it("should't remove file at path if it's not deleatable") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no such word deleatable
. You can use deletable
or delible
, but I recommend to use removable
here as it's less pretentious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: should't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a method in 'FileManager' called 'isDeleatableFileAtPath', I throught there is such word. 🙂
@thelvis4 Fixed typos. |
@mihai8804858 It seems like we introduced a bug here. <file name="/Some/path/to/file.swift"> but now we have <file /Some/path/to/file.swift=""> Could you please investigate and also write some tests to make sure the bug is fixed and we won't have regressions? |
expect(manager.fileExists(atPath: filePath)).to(beTrue()) | ||
FileManager.default.removeFileAtPath(filePath) | ||
} | ||
|
||
it("should't remove directory at path") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we have the same typo here: should't
@thelvis4 Done. |
@mihai8804858 How about the issue with |
Oops, I missed that comment. 😕 |
Slightly refactored reporter coordinators
@mihai8804858 Thanks for working on this! 👍 |
This PR will fix #48 issue.
Also, slightly refactored reporter coordinators.