Skip to content

Commit

Permalink
Move last legacy unit-test to esbuild (duckduckgo#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammacbeth authored Jul 26, 2023
1 parent b1565c4 commit 3cb58ff
Show file tree
Hide file tree
Showing 9 changed files with 9,893 additions and 14,019 deletions.
21 changes: 1 addition & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ endif
INTERMEDIATES_DIR = build/.intermediates

## All source files that potentially need to be bundled or copied.
# TODO: Use automatic dependency generation (e.g. `browserify --list`) for
# the bundling targets instead?
WATCHED_FILES = $(shell find -L browsers/ shared/ packages/ unit-test/ -type f -not -path "packages/*/node_modules/*" -not -name "*~")
# If the node_modules/@duckduckgo/ directory exists, include those source files
# in the list too.
Expand Down Expand Up @@ -68,7 +66,7 @@ watch:

## unit-test: Run the unit tests.
ESBUILD_TESTS = unit-test/background/*.js unit-test/background/**/*.js unit-test/ui/**/*.js unit-test/shared-utils/*.js
unit-test: build/test/legacy-background.js
unit-test:
$(ESBUILD) --outdir=build/test --inject:./unit-test/inject-chrome-shim.js $(ESBUILD_TESTS)
node_modules/.bin/karma start karma.conf.js

Expand Down Expand Up @@ -171,18 +169,9 @@ copy: $(LAST_COPY)
.PHONY: copy

###--- Build targets ---###
## Figure out the correct Browserify command for bundling.
# TODO: Switch to a better bundler.
# Workaround Browserify not following symlinks in --only.
BROWSERIFY_GLOBAL_TARGETS = ./node_modules/@duckduckgo
BROWSERIFY_GLOBAL_TARGETS += $(shell find node_modules/@duckduckgo/ -maxdepth 1 -type l | xargs -n1 readlink -f)

BROWSERIFY_BIN = node_modules/.bin/browserify
BROWSERIFY = $(BROWSERIFY_BIN) -t babelify -t [ babelify --global --only [ $(BROWSERIFY_GLOBAL_TARGETS) ] --plugins [ "./scripts/rewrite-meta" ] --presets [ @babel/preset-env ] ]
ESBUILD = node_modules/.bin/esbuild --bundle --target=firefox91,chrome92 --define:BUILD_TARGET=\"$(browser)\"
# Ensure sourcemaps are included for the bundles during development.
ifeq ($(type),dev)
BROWSERIFY += -d
ESBUILD += --sourcemap
endif

Expand Down Expand Up @@ -234,14 +223,6 @@ JS_BUNDLES = background.js base.js feedback.js options.js devtools-panel.js list

BUILD_TARGETS = $(addprefix $(BUILD_DIR)/public/js/, $(JS_BUNDLES))

## Unit tests scripts.
UNIT_TEST_SRC = unit-test/legacy/*.js
build/test:
mkdir -p $@

build/test/legacy-background.js: $(TEST_FILES) $(WATCHED_FILES) | build/test
$(BROWSERIFY) -t ./scripts/browserifyFileMapTransform $(UNIT_TEST_SRC) -o $@

## Content Scope Scripts
CONTENT_SCOPE_SCRIPTS = node_modules/@duckduckgo/content-scope-scripts

Expand Down
23,783 changes: 9,873 additions & 13,910 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,12 @@
"beta-chrome-mv3": "make chrome-mv3-beta browser=chrome-mv3 type=release"
},
"devDependencies": {
"@babel/cli": "^7.22.9",
"@babel/core": "^7.22.9",
"@babel/node": "^7.22.5",
"@babel/polyfill": "^7.10.4",
"@babel/preset-env": "^7.22.9",
"@fingerprintjs/fingerprintjs": "^3.4.2",
"@playwright/test": "^1.36.1",
"@types/chrome": "0.0.241",
"@types/jasmine": "^4.3.5",
"@types/webextension-polyfill": "^0.10.1",
"asana": "github:Asana/node-asana",
"babelify": "10.0.0",
"browserify": "^17.0.0",
"duckduckgo-colors": "0.0.1",
"esbuild": "^0.18.16",
"eslint": "^8.45.0",
Expand Down Expand Up @@ -93,18 +86,6 @@
"web-ext": "^7.6.2",
"zod": "^3.21.4"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"exclude": [
"transform-regenerator"
]
}
]
]
},
"workspaces": [
"packages/privacy-grade",
"packages/ddg2dnr"
Expand Down
29 changes: 0 additions & 29 deletions scripts/browserifyFileMapTransform.js

This file was deleted.

17 changes: 0 additions & 17 deletions scripts/rewrite-meta.js

This file was deleted.

7 changes: 3 additions & 4 deletions shared/js/background/dnr-config-rulesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getExtensionVersion, getManifestVersion } from './wrapper'
import settings from './settings'
import tdsStorage from './storage/tds'
import trackers from './trackers'
import { ready as startupReady } from './startup'
import { isFeatureEnabled } from './utils'
import {
ensureServiceWorkerInitiatedRequestExceptions
Expand Down Expand Up @@ -262,6 +261,7 @@ let ruleUpdateLock = Promise.resolve()
*/
export async function onConfigUpdate (configName, etag, configValue) {
const extensionVersion = getExtensionVersion()
console.log('update', configName, etag, configValue)
// Run an async lock on all blocklist updates so the latest update is always processed last
ruleUpdateLock = ruleUpdateLock.then(async () => {
// TDS (aka the block list).
Expand All @@ -272,9 +272,8 @@ export async function onConfigUpdate (configName, etag, configValue) {
return
}

await startupReady()
// @ts-ignore: Once startupReady() has finished, surrogateList will be
// assigned.
// All tds storage must have loaded before we can be sure that the surrogates are set
await tdsStorage.ready()
const supportedSurrogates = new Set(Object.keys(trackers.surrogateList))

const {
Expand Down
2 changes: 1 addition & 1 deletion shared/js/shared-utils/parse-user-agent-string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (uaString) => {
if (!globalThis.navigator) return
if (!globalThis.navigator) return {}

if (!uaString) uaString = globalThis.navigator.userAgent

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import '../helpers/mock-browser-api'
// TODO remove the * imports
import browser from 'webextension-polyfill'
// eslint-disable-next-line no-restricted-syntax
import * as allowlistedTrackers from '@duckduckgo/privacy-reference-tests/tracker-radar-tests/TR-domain-matching/tracker_allowlist_reference.json'
// eslint-disable-next-line no-restricted-syntax
import * as tds from '../data/tds.json'
// eslint-disable-next-line no-restricted-syntax
import * as browserWrapper from '../../shared/js/background/wrapper'
// eslint-disable-next-line no-restricted-syntax
import * as testConfig from '../data/extension-config.json'
// eslint-disable-next-line no-restricted-syntax
import * as tdsStorageStub from '../helpers/tds'
// eslint-disable-next-line no-restricted-syntax
import * as startup from '../../shared/js/background/startup'
import settings from '../../shared/js/background/settings'
import tabManager from '../../shared/js/background/tab-manager'
import tdsStorage from '../../shared/js/background/storage/tds'
Expand Down Expand Up @@ -183,10 +179,6 @@ describe('declarativeNetRequest', () => {
onUpdateListeners.set('tds', [onConfigUpdate])
tdsStorage.getLists().then(lists => trackers.setLists(lists))

spyOn(startup, 'ready').and.callFake(
() => Promise.resolve()
)

spyOn(settings, 'getSetting').and.callFake(
name => settingsStorage.get(name)
)
Expand Down Expand Up @@ -249,12 +241,10 @@ describe('declarativeNetRequest', () => {
() => Array.from(sessionRulesByRuleId.values())
)

spyOn(browserWrapper, 'getExtensionVersion').and.callFake(
() => extensionVersion
)
spyOn(browserWrapper, 'getManifestVersion').and.callFake(
() => 3
)
spyOn(browser.runtime, 'getManifest').and.callFake(() => ({
version: extensionVersion,
manifest_version: 3
}))
})

beforeEach(() => {
Expand Down
14 changes: 10 additions & 4 deletions unit-test/inject-chrome-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ const chrome = {
set: (value) => {
chrome.storage.local._setCalls.push(value)
},
get: () => {
return {}
get: (args, cb) => {
// eslint-disable-next-line n/no-callback-literal
cb({})
},
_setCalls: []
},
managed: {
get: () => {}
get: (args, cb) => {
// eslint-disable-next-line n/no-callback-literal
cb({})
}
}
},
browserAction: {
Expand All @@ -25,7 +29,9 @@ const chrome = {
runtime: {
id: '577dc9b9-c381-115a-2246-3f95fe0e6ffe',
sendMessage: () => {},
getManifest: () => ({ version: '1234.56' }),
getManifest: () => {
return { version: '1234.56' }
},
setUninstallURL: () => {},
getURL: path => path
},
Expand Down

0 comments on commit 3cb58ff

Please sign in to comment.