Skip to content

Commit

Permalink
add new test for utils (#36)
Browse files Browse the repository at this point in the history
* Remove unnecessary code

* Fix html template

* Add bookmarklets.html

* Add UtilsTests.swift

* Refactor

* �Add tearDownWithError

* Run `swiftlint --fix`

* Add proper setUpWithError and tearDownWithError

* Refactor Constants

* Refactor
  • Loading branch information
KS1019 authored Mar 6, 2022
1 parent 30f82a8 commit 8b152da
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/Commands/Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Init: ParsableCommand {

extension Init {
func run() throws {
let folder = try Constants.rootFolder
let folder = try Constants.homeFolder
.createSubfolderIfNeeded(at: Constants.hondanaDir)
.createSubfolderIfNeeded(at: Constants.bookmarkletsDir)
if folder.isEmpty() {
Expand Down
7 changes: 3 additions & 4 deletions Sources/Commands/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import Foundation
import Files

enum Constants {
static let rootDir = "~/"
static let hondanaDir = ".Hondana/"
static let bookmarkletsDir = "Bookmarklets/"

// swiftlint:disable force_try
static var rootFolder: Folder {
static var homeFolder: Folder {
if ProcessInfo.processInfo.environment["CI"] == nil
&& ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
return Folder.temporary
} else {
return try! Folder(path: rootDir)
return Folder.home
}
}

static let hondanaFolder: Folder = try! rootFolder.subfolder(at: hondanaDir)
static let hondanaFolder: Folder = try! homeFolder.subfolder(at: hondanaDir)
static let bookmarkletsFolder: Folder = try! hondanaFolder.subfolder(at: bookmarkletsDir)
}
2 changes: 1 addition & 1 deletion Sources/Commands/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Models
enum Utils {
static func generateHTML(from jsFiles: [File]) throws -> File {
let rawHTMLstring = """
<!doctype html
<!doctype html>
<html>
<title>Bookmarklets</title>
<h1>Bookmarklets</h1>
Expand Down
4 changes: 0 additions & 4 deletions Tests/ExtensionsTests/Files+ExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import Files
// swiftlint:disable line_length
final class FilesExtensionsTests: XCTestCase {


func testExtensions() throws {
let bookmarksHtmlFile = try File(path: #file).parent!.parent!.file(at: "Fixtures/+test.js")
print(bookmarksHtmlFile.nameExcludingExtension)
print(bookmarksHtmlFile.extension)
print(try bookmarksHtmlFile.readAsString())

XCTAssertTrue(bookmarksHtmlFile.isBookmarklet)
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixtures/bookmarklets.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!doctype html>
<html>
<title>Bookmarklets</title>
<h1>Bookmarklets</h1><a href="javascript:(alert(%22testing%22))();%0A">test</a></html>
17 changes: 9 additions & 8 deletions Tests/HondanaTests/HondanaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AssertSwiftCLI
final class HondanaTests: XCTestCase {
var hondaDirExistsPrev = false
override func setUpWithError() throws {
try Constants.rootFolder.createSubfolderIfNeeded(at: ".Hondana").createSubfolderIfNeeded(at: "Bookmarklets")
try Constants.homeFolder.createSubfolderIfNeeded(at: ".Hondana").createSubfolderIfNeeded(at: "Bookmarklets")
}

override func tearDownWithError() throws {
Expand All @@ -24,8 +24,8 @@ final class HondanaTests: XCTestCase {
try AssertExecuteCommand(command: "hondana list", expected: "No bookmarklet exist")

// 1 Bookmarklet
let bookmarksHtmlPath = try File(path: #file).parent!.parent!.url.appendingPathComponent("Fixtures/+test.js")
try Constants.bookmarkletsFolder.createFile(at: "+test.js", contents: try Data(contentsOf: bookmarksHtmlPath))
let bookmarkletJS = try File(path: #file).parent!.parent!.file(at: "Fixtures/+test.js")
try bookmarkletJS.copy(to: Constants.bookmarkletsFolder)
try AssertExecuteCommand(command: "hondana list", expected: """
+-------------------------------------------+
| Bookmarklets |
Expand Down Expand Up @@ -83,23 +83,24 @@ final class HondanaTests: XCTestCase {
return Bundle.main.bundleURL
#endif
}
}

extension XCTest {
enum Constants {
static let rootDir = "~/"
static let hondanaDir = ".Hondana/"
static let bookmarkletsDir = "Bookmarklets/"

// swiftlint:disable force_try
static var rootFolder: Folder {
static var homeFolder: Folder {
if ProcessInfo.processInfo.environment["CI"] == nil
&& ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
return Folder.temporary
} else {
return try! Folder(path: rootDir)
return Folder.home
}
}

static let hondanaFolder: Folder = try! rootFolder.subfolder(at: hondanaDir)
static let bookmarkletsFolder: Folder = try! hondanaFolder.subfolder(at: bookmarkletsDir)
static let hondanaFolder: Folder = try! homeFolder.createSubfolderIfNeeded(at: hondanaDir)
static let bookmarkletsFolder: Folder = try! hondanaFolder.createSubfolderIfNeeded(at: bookmarkletsDir)
}
}
50 changes: 50 additions & 0 deletions Tests/HondanaTests/UtitlsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation
import Extensions
import XCTest
import Files
import AssertSwiftCLI

final class UtilsTests: XCTestCase {
override func setUpWithError() throws {
try Constants.homeFolder.createSubfolderIfNeeded(at: ".Hondana").createSubfolderIfNeeded(at: "Bookmarklets")
}

override func tearDownWithError() throws {
try Constants.hondanaFolder.delete()
}

enum Utils {
static func generateHTML(from jsFiles: [File]) throws -> File {
let rawHTMLstring = """
<!doctype html>
<html>
<title>Bookmarklets</title>
<h1>Bookmarklets</h1>
""" +
(try jsFiles
.map {
aTag(url: try $0.readAsString(encodedAs: .utf8).withJSPrefix.minified,
title: $0.nameExcludingExtension.components(separatedBy: "+")[1])
}
.joined(separator: "\n")
)
+ """
</html>
"""
return try Constants.hondanaFolder
.createFile(at: "bookmarklets.html", contents: rawHTMLstring.data(using: .utf8))
}

private static func aTag(url: String, title: String) -> String {
return "<a href=\"\(url)\">\(title)</a>"
}
}

func testGenerateHTML() throws {
let bookmarkJS = try File(path: #file).parent!.parent!.file(at: "Fixtures/+test.js")
let bookmarkHtml = try File(path: #file).parent!.parent!.file(at: "Fixtures/bookmarklets.html")

let file = try Utils.generateHTML(from: [bookmarkJS])
XCTAssertEqual(try file.readAsString().trimmingLines(), String(try bookmarkHtml.readAsString().trimmingLines().dropLast()))
}
}

0 comments on commit 8b152da

Please sign in to comment.