|
| 1 | +jest.mock("danger", () => jest.fn()) |
| 2 | +import danger from 'danger' |
| 3 | +const dm = danger as any; |
| 4 | + |
| 5 | +import { singletons } from '../org/allPRs' |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + dm.addedLines = "" |
| 9 | + dm.fail = jest.fn().mockReturnValue(true); |
| 10 | + |
| 11 | + dm.danger = { |
| 12 | + git: { |
| 13 | + diffForFile: async (_filename) => { |
| 14 | + return { added: dm.addedLines } |
| 15 | + }, |
| 16 | + modified_files: [ |
| 17 | + "ModifiedFile.swift" |
| 18 | + ], |
| 19 | + created_files: [ |
| 20 | + "CreatedFile.swift" |
| 21 | + ] |
| 22 | + } |
| 23 | + } |
| 24 | +}) |
| 25 | + |
| 26 | +describe("Singletons checks", () => { |
| 27 | + it("does not fail with no changes to Swift files", async () => { |
| 28 | + dm.danger.git.modified_files = [] |
| 29 | + |
| 30 | + await singletons() |
| 31 | + expect(dm.fail).not.toHaveBeenCalled() |
| 32 | + }) |
| 33 | + |
| 34 | + it("does not fail with no diff in Swift files", async () => { |
| 35 | + dm.danger.git.diffForFile = async (_filename) => {} |
| 36 | + |
| 37 | + await singletons() |
| 38 | + expect(dm.fail).not.toHaveBeenCalled() |
| 39 | + }) |
| 40 | + |
| 41 | + it("does not fail with no additions", async () => { |
| 42 | + await singletons() |
| 43 | + expect(dm.fail).not.toHaveBeenCalled() |
| 44 | + }) |
| 45 | + |
| 46 | + it("does not fail with non-singleton additions", async () => { |
| 47 | + dm.addedLines = ` |
| 48 | ++ let subscription = PrivacyProSubscription(status: .inactive) |
| 49 | ++ let isSubscribed: Bool |
| 50 | ++ var billingPeriod: String? |
| 51 | ++ public let startedAt: Int? |
| 52 | ++ internal let expiresOrRenewsAt: Int? |
| 53 | ++ private var paymentPlatform: String? |
| 54 | ++ let status: String? |
| 55 | + ` |
| 56 | + |
| 57 | + await singletons() |
| 58 | + expect(dm.fail).not.toHaveBeenCalled() |
| 59 | + }) |
| 60 | + |
| 61 | + it("does not fail with singleton usage additions", async () => { |
| 62 | + dm.addedLines = ` |
| 63 | ++ let favicon = FaviconManager.shared.favicon(for: url) |
| 64 | + ` |
| 65 | + |
| 66 | + await singletons() |
| 67 | + expect(dm.fail).not.toHaveBeenCalled() |
| 68 | + }) |
| 69 | + |
| 70 | + it("does not fail with commented out singleton additions", async () => { |
| 71 | + dm.addedLines = ` |
| 72 | ++ // static let shared = FaviconManager() |
| 73 | + ` |
| 74 | + |
| 75 | + await singletons() |
| 76 | + expect(dm.fail).not.toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it("fails with singleton additions", async () => { |
| 80 | + dm.addedLines = ` |
| 81 | ++ static let shared = FaviconManager() |
| 82 | + ` |
| 83 | + |
| 84 | + await singletons() |
| 85 | + expect(dm.fail).toHaveBeenCalled() |
| 86 | + }) |
| 87 | + |
| 88 | + it("fails with singleton additions with type specifier", async () => { |
| 89 | + dm.addedLines = ` |
| 90 | ++ static let shared: FaviconManager = .init() |
| 91 | + ` |
| 92 | + |
| 93 | + await singletons() |
| 94 | + expect(dm.fail).toHaveBeenCalled() |
| 95 | + }) |
| 96 | + |
| 97 | + it("fails with private singleton additions", async () => { |
| 98 | + dm.addedLines = ` |
| 99 | ++ private static let shared = FaviconManager() |
| 100 | + ` |
| 101 | + |
| 102 | + await singletons() |
| 103 | + expect(dm.fail).toHaveBeenCalled() |
| 104 | + }) |
| 105 | + |
| 106 | + it("fails with internal singleton additions", async () => { |
| 107 | + dm.addedLines = ` |
| 108 | ++ internal static let shared = FaviconManager() |
| 109 | + ` |
| 110 | + |
| 111 | + await singletons() |
| 112 | + expect(dm.fail).toHaveBeenCalled() |
| 113 | + }) |
| 114 | + |
| 115 | + it("fails with public singleton additions", async () => { |
| 116 | + dm.addedLines = ` |
| 117 | ++ public static let shared = FaviconManager() |
| 118 | + ` |
| 119 | + |
| 120 | + await singletons() |
| 121 | + expect(dm.fail).toHaveBeenCalled() |
| 122 | + }) |
| 123 | + |
| 124 | + it("fails with var singleton additions", async () => { |
| 125 | + dm.addedLines = ` |
| 126 | ++ static var shared = FaviconManager() |
| 127 | + ` |
| 128 | + |
| 129 | + await singletons() |
| 130 | + expect(dm.fail).toHaveBeenCalled() |
| 131 | + }) |
| 132 | + |
| 133 | + it("fails with private var singleton additions", async () => { |
| 134 | + dm.addedLines = ` |
| 135 | ++ private static var shared = FaviconManager() |
| 136 | + ` |
| 137 | + |
| 138 | + await singletons() |
| 139 | + expect(dm.fail).toHaveBeenCalled() |
| 140 | + }) |
| 141 | + |
| 142 | + it("fails with internal var singleton additions", async () => { |
| 143 | + dm.addedLines = ` |
| 144 | ++ internal static var shared = FaviconManager() |
| 145 | + ` |
| 146 | + |
| 147 | + await singletons() |
| 148 | + expect(dm.fail).toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + it("fails with public var singleton additions", async () => { |
| 152 | + dm.addedLines = ` |
| 153 | ++ public static var shared = FaviconManager() |
| 154 | + ` |
| 155 | + |
| 156 | + await singletons() |
| 157 | + expect(dm.fail).toHaveBeenCalled() |
| 158 | + }) |
| 159 | + |
| 160 | + it("does not fail with singleton deletions", async () => { |
| 161 | + dm.addedLines = ` |
| 162 | +- static let shared = FaviconManager() |
| 163 | +- private static let shared = FaviconManager() |
| 164 | +- internal static let shared = FaviconManager() |
| 165 | +- public static let shared = FaviconManager() |
| 166 | +- static var shared = FaviconManager() |
| 167 | +- private static var shared = FaviconManager() |
| 168 | +- internal static var shared = FaviconManager() |
| 169 | +- public static var shared = FaviconManager() |
| 170 | + ` |
| 171 | + |
| 172 | + await singletons() |
| 173 | + expect(dm.fail).not.toHaveBeenCalled() |
| 174 | + }) |
| 175 | +}) |
0 commit comments