Skip to content

Commit

Permalink
Merge branch 'master' into fix/AG-22258
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed May 26, 2023
2 parents acc928a + aee79e0 commit 3a4301b
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- ability for `prevent-element-src-loading` scriptlet to prevent inline `onerror` and match `link` tag [#276](https://github.com/AdguardTeam/Scriptlets/issues/276)
- new special value modifiers for `set-constant` [#316](https://github.com/AdguardTeam/Scriptlets/issues/316)

### Changed

Expand Down
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,33 @@ const isBlocking = redirect.isBlocking('click2load.html');
## <a id="how-to-test"></a> How to test

Run node testing
```
yarn test
```

Run tests gui
```
yarn gui-test
```
- for all tests:

```
yarn test
```

- for limited testing:

```
yarn test api
yarn test helpers
yarn test scriptlets
yarn test redirects
```

- for scriptlets and redirects test run can be more specific:

```
// node test run
yarn test scriptlets --name set-cookie
yarn test redirects --name ati-smarttag

// gui test run
yarn test scriptlets --name set-cookie --gui
yarn test redirects --name ati-smarttag --gui
```

Watcher is available
```
Expand Down
2 changes: 1 addition & 1 deletion bamboo-specs/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plan:
key: SCRIPTLETSBUILD
name: scriptlets - build
variables:
dockerContainer: adguard/puppeteer-runner:8.0--1
dockerContainer: adguard/puppeteer-runner:18.2--1

stages:
- Build:
Expand Down
2 changes: 1 addition & 1 deletion bamboo-specs/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plan:
key: SCRIPTLETSTEST
name: scriptlets - test new
variables:
dockerPuppeteer: adguard/puppeteer-runner:8.0--1
dockerPuppeteer: adguard/puppeteer-runner:18.2--1

stages:
- Build:
Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
silent: true,
};

module.exports = config;
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"name": "@adguard/scriptlets",
"version": "1.9.19",
"version": "1.9.25",
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
"scripts": {
"build": "babel-node bundler.js",
"build-tests": "yarn build && babel-node scripts/build-tests.js",
"test": "yarn build-tests && node tests/index.js && yarn jest",
"browserstack": "babel-node scripts/build-tests.js && node browserstack.js",
"gui-test": "babel-node scripts/build-tests.js && open http://localhost:8585 && node ./tests/server.js",
"build": "babel-node scripts/build.js",
"test": "yarn jest && babel-node scripts/test.js",
"browserstack": "yarn test --build && node browserstack.js",
"jest": "jest trusted-set-cookie-reload.spec.js",
"lint": "eslint .",
"lint-staged": "lint-staged",
Expand Down Expand Up @@ -76,6 +74,7 @@
"kleur": "^4.1.4",
"lint-staged": "^12.1.2",
"node-qunit-puppeteer": "2.1.1",
"openurl": "^1.1.1",
"qunit": "^2.9.3",
"rollup": "^2.70.2",
"rollup-plugin-cleanup": "^3.1.1",
Expand Down
132 changes: 84 additions & 48 deletions scripts/build-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,38 @@ import copy from 'rollup-plugin-copy';
import json from '@rollup/plugin-json';
import generateHtml from 'rollup-plugin-generate-html';
import { rollupStandard } from './rollup-runners';
import { generateHtmlTestFilename } from './helpers';

const TESTS_DIST = path.resolve(__dirname, '../tests/dist');
const TESTS_DIR = '../tests';
const TESTS_DIST = path.resolve(__dirname, TESTS_DIR, 'dist');
const TEST_FILE_NAME_MARKER = '.test.js';

// TODO: use jest for api methods tests. AG-22558
const SINGLE_TEST_FILE_DIRS = [
'api',
];
const MULTIPLE_TEST_FILES_DIRS = [
'scriptlets',
'redirects',
'helpers',
];

/**
* Prepares rollup config for test file
*
* @param {string} fileName test file name
* @param {string} dirPath resolved directory path
* @param {string} subDir subdirectory with test files
* @returns {Object} rollup config
*/
const getTestConfig = (fileName, dirPath, subDir) => {
const getTestConfig = (fileName, subDir) => {
if (!fs.existsSync(TESTS_DIST)) {
fs.mkdirSync(TESTS_DIST);
} else {
fs.emptyDirSync(TESTS_DIST);
}

const dirPath = path.resolve(__dirname, TESTS_DIR, subDir);

// cut off '.test.js' test file name ending
const finalFileName = fileName.slice(0, -TEST_FILE_NAME_MARKER.length);
return ({
Expand Down Expand Up @@ -56,7 +69,7 @@ const getTestConfig = (fileName, dirPath, subDir) => {
generateHtml({
// add subDir to final file name to avoid files rewriting
// because there are some equal file names in different directories
filename: `${TESTS_DIST}/${subDir}-${finalFileName}.html`,
filename: `${TESTS_DIST}/${generateHtmlTestFilename(subDir, finalFileName)}`,
template: 'tests/template.html',
selector: 'body',
inline: true,
Expand All @@ -78,55 +91,78 @@ const getTestConfig = (fileName, dirPath, subDir) => {
});
};

const getTestConfigs = () => {
const TESTS_DIR = '../tests';
const MULTIPLE_TEST_FILES_DIRS = [
'scriptlets',
'redirects',
'helpers',
];
const ONE_TEST_FILE_DIRS = [
'lib-tests',
];
/**
* Returns list of file names in <repo root>/tests/`subDir` which has `.test.js` ending.
*
* @param {string} subDir Subdirectory with test files.
*
* @returns {string[]} List of test files.
*/
const getTestFilesFromDir = (subDir) => {
const dirPath = path.resolve(__dirname, TESTS_DIR, subDir);
return fs.readdirSync(dirPath, { encoding: 'utf8' })
.filter((el) => el.includes(TEST_FILE_NAME_MARKER));
};

const multipleFilesConfigs = MULTIPLE_TEST_FILES_DIRS
.map((subDir) => {
const dirPath = path.resolve(__dirname, TESTS_DIR, subDir);
const filesList = fs.readdirSync(dirPath, { encoding: 'utf8' })
.filter((el) => {
// skip index files for scriptlets and redirects
return el !== 'index.test.js'
// for testing specific scriptlet or redirect you should filter by its name
// or just uncomment next line and fix test name
// && el === 'gemius.test.js'
// please note that oneFileConfigs will still be run
&& el.includes(TEST_FILE_NAME_MARKER);
});
return filesList.map((filename) => getTestConfig(filename, dirPath, subDir));
})
.flat(1);
/**
* Returns list of file names in <repo root>/tests/`subDir` which has `.test.js` ending
* except `index.test.js`.
*
* @param {string} subDir Subdirectory with test files.
*
* @returns {string[]} List of test files.
*/
const getMultipleTestFilesFromDir = (subDir) => {
return getTestFilesFromDir(subDir)
.filter((el) => el !== 'index.test.js');
};

const oneFileConfigs = ONE_TEST_FILE_DIRS
.map((subDir) => {
const dirPath = path.resolve(__dirname, TESTS_DIR, subDir);
const filesList = fs.readdirSync(dirPath, { encoding: 'utf8' })
.filter((el) => el.includes(TEST_FILE_NAME_MARKER));
return filesList.map((filename) => getTestConfig(filename, dirPath, subDir));
})
.flat(1);
/**
* Returns list of rollup configs for tests.
*
* @param {Object} limitData Optional data object for limited tests running. If not provided, all tests will be run.
* @param {string} limitData.type Type of tests to run: scriptlets | redirects | helpers | api.
* @param {string} limitData.name Optional name scriptlets or redirects test to run.
*
* @returns {Object[]} Array of rollup configs for tests.
*/
const getTestConfigs = (limitData) => {
// run limited list of tests if limitData is provided
if (limitData && limitData.type) {
const { type } = limitData;
// TODO: this checking may be ditched
// when api methods tests are run by jest. AG-22558
let filesList = MULTIPLE_TEST_FILES_DIRS.includes(type)
? getMultipleTestFilesFromDir(type)
: getTestFilesFromDir(type);

const configs = [
...oneFileConfigs,
...multipleFilesConfigs,
];
const { name } = limitData;
if (name) {
filesList = filesList.filter((el) => el === `${name}${TEST_FILE_NAME_MARKER}`);
}

return configs;
};
return filesList.map((filename) => getTestConfig(filename, type));
}

const buildTests = async () => {
const testConfigs = getTestConfigs();
// otherwise run all tests
const allConfigs = [];

await rollupStandard(testConfigs);
MULTIPLE_TEST_FILES_DIRS.forEach((subDir) => {
getMultipleTestFilesFromDir(subDir).forEach((filename) => {
allConfigs.push(getTestConfig(filename, subDir));
});
});

SINGLE_TEST_FILE_DIRS.forEach((subDir) => {
getTestFilesFromDir(subDir).forEach((filename) => {
allConfigs.push(getTestConfig(filename, subDir));
});
});

return allConfigs;
};

buildTests();
export const buildTests = async (limitData) => {
const testConfigs = getTestConfigs(limitData);
await rollupStandard(testConfigs);
};
21 changes: 8 additions & 13 deletions bundler.js → scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { program } from 'commander';

import { buildScriptletsFunc } from './scripts/build-funcs';
import { buildScriptletsFunc } from './build-funcs';
import {
buildClick2Load,
buildRedirectsFiles,
buildRedirectsForCorelibs,
buildRedirectsList,
prebuildRedirects,
} from './scripts/build-redirects';
import { buildScriptletsForCorelibs } from './scripts/build-corelibs';
import { buildScriptlets, buildScriptletsList } from './scripts/build-scriptlets';
import { buildTxt } from './scripts/build-txt';
import { buildRedirectsMap } from './scripts/build-redirects-map';
} from './build-redirects';
import { buildScriptletsForCorelibs } from './build-corelibs';
import { buildScriptlets, buildScriptletsList } from './build-scriptlets';
import { buildTxt } from './build-txt';
import { buildRedirectsMap } from './build-redirects-map';
import { runTasks } from './helpers';

const buildScriptletsAndRedirectsLists = async () => {
await Promise.all([buildRedirectsList(), buildScriptletsList()]);
Expand All @@ -30,14 +31,8 @@ const tasks = [
buildTxt,
];

const runTasks = async (tasks, options) => {
for (const task of tasks) {
await task(options);
}
};

program
.description('Builds scriptlets')
.description('Builds scriptlets and redirects')
.action(async () => {
await runTasks(tasks);
});
Expand Down
5 changes: 4 additions & 1 deletion scripts/check-sources-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ async function getCurrentUBOScriptlets() {
.slice(ALIASES_MARKER.length)
// prepare the string for JSON.parse
.replace(/'/g, '"')
.replace(/,?$/, '');
.replace(/,?$/, '')
// remove comments
// e.g. "[ 'rnt.js', 'sed.js' /* to be removed */ ]"
.replace(/\/\*[\s|\w]+?\*\//, '');
aliases = JSON.parse(aliasesStr);
// 'name' string goes first and 'aliases' string goes after it
// so if aliases are parsed, no need to continue lines iterating
Expand Down
12 changes: 12 additions & 0 deletions scripts/compatibility-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@
},
{
"ubo": "window.name-defuser.js"
},
{
"ubo": "spoof-css.js"
},
{
"ubo": "remove-node-text.js (rmnt.js)"
},
{
"ubo": "replace-node-text.js (rpnt.js, sed.js)"
},
{
"ubo": "trusted-set-constant.js (trusted-set.js)"
}
],
"redirects": [
Expand Down
15 changes: 15 additions & 0 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,25 @@ const getDataFromFiles = (filesList, relativeDirPath) => {
});
};

const runTasks = async (tasks) => {
for (const task of tasks) {
await task();
}
};

const generateHtmlTestFilename = (type, name) => {
if (!type || !name) {
throw new Error('type and name are required');
}
return `${type}-${name}.html`;
};

module.exports = {
writeFile,
getFilesList,
getDataFromFiles,
runTasks,
generateHtmlTestFilename,
SCRIPTLET_TYPE,
TRUSTED_SCRIPTLET_TYPE,
REDIRECT_TYPE,
Expand Down
Loading

0 comments on commit 3a4301b

Please sign in to comment.