From 60c761a9cec52a1e0ec85386c87298aa03bf2a58 Mon Sep 17 00:00:00 2001 From: Thanh Vu Date: Tue, 31 Oct 2023 20:52:26 +0700 Subject: [PATCH] Add unit test and example --- .../Configs/legacy/Debug.xcconfig | 1 + .../Configs/legacy/Release.xcconfig | 1 + .../ContentBlockerRequestHandler.swift | 22 +++++++++++++++ .../TestProject/ContentBlocker/Info.plist | 13 +++++++++ .../ContentBlocker/blockerList.json | 10 +++++++ Tests/Fixtures/TestProject/project.yml | 19 ++++++++++++- .../global_template_attributes_test.yml | 28 +++++++++++++++++++ Tests/ProjectSpecTests/SpecLoadingTests.swift | 9 ++++++ 8 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Debug.xcconfig create mode 100644 Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Release.xcconfig create mode 100644 Tests/Fixtures/TestProject/ContentBlocker/ContentBlockerRequestHandler.swift create mode 100644 Tests/Fixtures/TestProject/ContentBlocker/Info.plist create mode 100644 Tests/Fixtures/TestProject/ContentBlocker/blockerList.json create mode 100644 Tests/Fixtures/global_template_attributes_test.yml diff --git a/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Debug.xcconfig b/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Debug.xcconfig new file mode 100644 index 00000000..9a239976 --- /dev/null +++ b/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Debug.xcconfig @@ -0,0 +1 @@ +API_PATH = https://dev.google.com diff --git a/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Release.xcconfig b/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Release.xcconfig new file mode 100644 index 00000000..4353da07 --- /dev/null +++ b/Tests/Fixtures/TestProject/ContentBlocker/Configs/legacy/Release.xcconfig @@ -0,0 +1 @@ +API_PATH = https://release.google.com diff --git a/Tests/Fixtures/TestProject/ContentBlocker/ContentBlockerRequestHandler.swift b/Tests/Fixtures/TestProject/ContentBlocker/ContentBlockerRequestHandler.swift new file mode 100644 index 00000000..cf2e6083 --- /dev/null +++ b/Tests/Fixtures/TestProject/ContentBlocker/ContentBlockerRequestHandler.swift @@ -0,0 +1,22 @@ +// +// ContentBlockerRequestHandler.swift +// ContentBlocker +// +// Created by Thanh Vu on 31/10/2023. +// + +import UIKit +import MobileCoreServices + +class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling { + + func beginRequest(with context: NSExtensionContext) { + let attachment = NSItemProvider(contentsOf: Bundle.main.url(forResource: "blockerList", withExtension: "json"))! + + let item = NSExtensionItem() + item.attachments = [attachment] + + context.completeRequest(returningItems: [item], completionHandler: nil) + } + +} diff --git a/Tests/Fixtures/TestProject/ContentBlocker/Info.plist b/Tests/Fixtures/TestProject/ContentBlocker/Info.plist new file mode 100644 index 00000000..a2f64a60 --- /dev/null +++ b/Tests/Fixtures/TestProject/ContentBlocker/Info.plist @@ -0,0 +1,13 @@ + + + + + NSExtension + + NSExtensionPointIdentifier + com.apple.Safari.content-blocker + NSExtensionPrincipalClass + $(PRODUCT_MODULE_NAME).ContentBlockerRequestHandler + + + diff --git a/Tests/Fixtures/TestProject/ContentBlocker/blockerList.json b/Tests/Fixtures/TestProject/ContentBlocker/blockerList.json new file mode 100644 index 00000000..a0fd0c72 --- /dev/null +++ b/Tests/Fixtures/TestProject/ContentBlocker/blockerList.json @@ -0,0 +1,10 @@ +[ + { + "action": { + "type": "block" + }, + "trigger": { + "url-filter": "webkit.svg" + } + } +] diff --git a/Tests/Fixtures/TestProject/project.yml b/Tests/Fixtures/TestProject/project.yml index f6666784..1b1ddfd5 100644 --- a/Tests/Fixtures/TestProject/project.yml +++ b/Tests/Fixtures/TestProject/project.yml @@ -16,6 +16,11 @@ options: buildPhase: none abcd: buildPhase: none +globalTemplateAttributes: + version: legacy +configs: + Debug: debug + Release: release fileGroups: - Configs - FileGroup @@ -79,7 +84,9 @@ targets: legacy: toolPath: /usr/bin/true passSettings: true - + ContentBlocker: + templates: + - ContentBlockerTemplate App_macOS: type: application platform: macOS @@ -497,6 +504,16 @@ schemes: targetTemplates: MyTemplate: scheme: {} + ContentBlockerTemplate: + platform: iOS + type: app-extension + settings: + CODE_SIGN_IDENTITY: Apple Development + configFiles: + Debug: ContentBlocker/Configs/${version}/Debug.xcconfig + Release: ContentBlocker/Configs/${version}/Release.xcconfig + sources: + - ContentBlocker aggregateTargets: SuperTarget: attributes: diff --git a/Tests/Fixtures/global_template_attributes_test.yml b/Tests/Fixtures/global_template_attributes_test.yml new file mode 100644 index 00000000..c524c11d --- /dev/null +++ b/Tests/Fixtures/global_template_attributes_test.yml @@ -0,0 +1,28 @@ +targetTemplates: + ContentBlockerTemplate: + platform: iOS + type: app-extension + settings: + CODE_SIGN_IDENTITY: Apple Development + configFiles: + Debug: ContentBlocker/Configs/${version}/Debug.xcconfig + Release: ContentBlocker/Configs/${version}/Release.xcconfig + sources: + - ContentBlocker + +globalTemplateAttributes: + version: legacy + +name: Demo +options: + createIntermediateGroups: True +targets: + Demo: + type: application + platform: iOS + deploymentTarget: "10.0" + sources: + - DemoXcodeGenGlobalTemplateAttribute + ContentBlocker: + templates: + - ContentBlockerTemplate \ No newline at end of file diff --git a/Tests/ProjectSpecTests/SpecLoadingTests.swift b/Tests/ProjectSpecTests/SpecLoadingTests.swift index bb8a4b18..6a418ce3 100644 --- a/Tests/ProjectSpecTests/SpecLoadingTests.swift +++ b/Tests/ProjectSpecTests/SpecLoadingTests.swift @@ -285,6 +285,15 @@ class SpecLoadingTests: XCTestCase { ] try expect(project.targets.last?.sources) == ["SomeTarget", "doesWin", "templateVariable"] } + + $0.it("lookup global template attributes") { + let path = fixturePath + "global_template_attributes_test.yml" + + let project = try loadSpec(path: path) + let extensionTarget = project.targets.first! + try expect(extensionTarget.configFiles["Debug"]) == "ContentBlocker/Configs/legacy/Debug.xcconfig" + try expect(extensionTarget.configFiles["Release"]) == "ContentBlocker/Configs/legacy/Release.xcconfig" + } } }