From 2aaaba14812e473c23bf77b945ea6e986149ee82 Mon Sep 17 00:00:00 2001 From: hoangatuan Date: Sat, 2 Dec 2023 18:00:24 +0800 Subject: [PATCH] Refactor and fix issue temporary is not deleted if execute danger fail --- .../Sources/LeaksDetector/LeaksDetector.swift | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/LeaksDetector/Sources/LeaksDetector/LeaksDetector.swift b/LeaksDetector/Sources/LeaksDetector/LeaksDetector.swift index 1978316..f8ca628 100644 --- a/LeaksDetector/Sources/LeaksDetector/LeaksDetector.swift +++ b/LeaksDetector/Sources/LeaksDetector/LeaksDetector.swift @@ -40,15 +40,18 @@ struct LeaksDetector: ParsableCommand { log(message: "Start looking for process with name: \(processName)... ๐Ÿ”Ž") + /// Step 1: Using UI Testing tool to simulate the flow if !simulateUIFlow(by: executor) { Darwin.exit(EXIT_FAILURE) } + /// Step 2: Using *leak* tool provided by Apple to generate a memgrpah file if !generateMemgraph(by: executor) { Darwin.exit(EXIT_FAILURE) } do { + /// Step 3: Using *leak* tool provided by Apple to process generated memgraph from Step2. try checkLeaks(by: executor) } catch { log(message: "โŒ Error occurs while checking for leaks", color: .red) @@ -56,12 +59,6 @@ struct LeaksDetector: ParsableCommand { } } - private func prepareParams() -> ExecutorParameters { - var params: [String: String] = [:] - params[ParameterKeys.maestroFilePath] = maestroFlowPath - return params - } - private func simulateUIFlow(by executor: Executor) -> Bool { log(message: "Start running ui flow... ๐ŸŽฅ") do { @@ -89,6 +86,8 @@ struct LeaksDetector: ParsableCommand { do { log(message: "Start checking for leaks... โš™๏ธ") let memgraphPath = executor.getMemgraphPath() + + /// Running this script always throw error (somehow the leak tool throw error here) => So we need to process the memgraph in the `catch` block. try shellOut(to: "leaks", arguments: ["\(memgraphPath) -q"]) } catch { let error = error as! ShellOutError @@ -104,25 +103,35 @@ struct LeaksDetector: ParsableCommand { } // Create a file to store the message, so that later Danger can read from that file - // TODO: Convert to 1 message instead of using array-for loop to optimize execution time let fileName = "temporary.txt" for message in inputs { let updatedMessage = "\"\(message)\"" try shellOut(to: "echo \(updatedMessage) >> \(fileName)") } - - // Cache memgraphfile if need + + // Send memgraph to remote storage if need log(message: "Founded leaks. Generating reports... โš™๏ธ") - try shellOut(to: "bundle exec danger --dangerfile=\(dangerPath) --danger_id=LeaksReport") - log(message: "Cleaning... ๐Ÿงน") - _ = try? shellOut(to: "rm \(executor.getMemgraphPath())") - _ = try? shellOut(to: "rm \(fileName)") + do { + try shellOut(to: "bundle exec danger --dangerfile=\(dangerPath) --danger_id=LeaksReport") + log(message: "Done โœ…", color: .green) + } catch { + log(message: "โŒ Can not execute Danger", color: .red) + } - log(message: "Done โœ…", color: .green) + cleanup(executor: executor, fileName: fileName) } } +} + +// MARK: - Helper functions +extension LeaksDetector { + private func prepareParams() -> ExecutorParameters { + var params: [String: String] = [:] + params[ParameterKeys.maestroFilePath] = maestroFlowPath + return params + } private func getNumberOfLeaks(from message: String) -> Int { if let regex = try? NSRegularExpression(pattern: regex, options: []) { @@ -138,6 +147,11 @@ struct LeaksDetector: ParsableCommand { return 0 } + private func cleanup(executor: Executor, fileName: String) { + log(message: "Cleaning... ๐Ÿงน") + _ = try? shellOut(to: "rm \(executor.getMemgraphPath())") + _ = try? shellOut(to: "rm \(fileName)") + } } extension String {