Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jellizaveta committed Dec 3, 2024
1 parent 569ff8f commit eae8b8e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 78 deletions.
109 changes: 109 additions & 0 deletions __tests__/filters-downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,116 @@ import nock from 'nock';
import { FiltersDownloader } from '../src';
import { server } from './server';

const FilterCompilerConditionsConstants = {
adguard: true,
adguard_ext_chromium: true,
adguard_ext_firefox: false,
adguard_ext_edge: false,
adguard_ext_safari: false,
adguard_ext_opera: false,
adguard_ext_android_cb: false,
};

describe('FiltersDownloader', () => {
describe('error message during `!#include` and condition directives resolving', () => {
it('condition end not found', async () => {
const rules = [
'always_included_rule',
'!#if (adguard)',
'if_adguard_included_rule',
];
expect(() => FiltersDownloader.resolveConditions(
rules,
FilterCompilerConditionsConstants,
)).toThrowError(new Error(`Invalid directives: Condition end not found '!#if (adguard)'
Context:
\talways_included_rule
\t!#if (adguard)
`));
});
it('unexpected condition end', async () => {
const rules = [
'always_included_rule',
'if_adguard_included_rule',
'!#endif',
];
expect(() => FiltersDownloader.resolveConditions(
rules,
FilterCompilerConditionsConstants,
)).toThrowError(new Error(`Invalid directives: Found unexpected condition end: '!#endif'
Context:
\talways_included_rule
\tif_adguard_included_rule
\t!#endif
`));
});
it('failed to resolve the include directive', async () => {
const rules = [
'always_included_rule',
'||example.com',
'||example.org',
'!#include resources/not_found_file.txt',
];
await expect(FiltersDownloader.resolveIncludes(
rules,
undefined,
FilterCompilerConditionsConstants,
)).rejects.toThrowError(
new Error(`Failed to resolve the include directive '!#include resources/not_found_file.txt'
Context:
\talways_included_rule
\t||example.com
\t||example.org
\t!#include resources/not_found_file.txt
`),
);
});
it('failed to resolve the include directive without file path', async () => {
const rules = [
'always_included_rule',
'included_rule',
'||example.org^',
'||example.com^',
'!#include',
];
await expect(FiltersDownloader.resolveIncludes(
rules,
undefined,
FilterCompilerConditionsConstants,
)).rejects.toThrowError(
new Error(`Failed to resolve the include directive '!#include'
Context:
\tincluded_rule
\t||example.org^
\t||example.com^
\t!#include
`),
);
});
it('failed to resolve the include directive without file path', async () => {
const rules = [
'always_included_rule',
'||example.org^',
'!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt',
'if_adguard_included_rule',
'||example.com^',
];
await expect(
FiltersDownloader.compile(
rules,
undefined,
FilterCompilerConditionsConstants,
),
).rejects.toThrowError(
new Error(`Failed to resolve the include directive '!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt'
Context:
\talways_included_rule
\t||example.org^
\t!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt
`),
);
});
});
describe('downloadWithRaw', () => {
describe('applies patches', () => {
beforeAll(async () => {
Expand Down
69 changes: 2 additions & 67 deletions _qunit_tests_/filters-downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,6 @@ QUnit.test('Test filter downloader - simple "if" conditions', async (assert) =>
const { FiltersDownloader } = require('../dist');
assert.ok(FiltersDownloader);

rules = [
'always_included_rule',
'!#if (adguard)',
];

assert.throws(() => {
FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants);
}, `Error: Invalid directives: Condition end not found '!#if (adguard)'
Context:
\talways_included_rule
\t!#if (adguard)
`);

rules = [
'always_included_rule',
'if_adguard_included_rule',
'!#endif',
];

assert.throws(() => {
FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants);
}, `Error: Invalid directives: Found unexpected condition end: '!#endif'
Context:
\talways_included_rule
\tif_adguard_included_rule
\t!#endif`);

rules = [
'always_included_rule',
'!#if (adguard)',
Expand Down Expand Up @@ -702,31 +675,11 @@ QUnit.test('Test filter downloader - external download and includes with special
});

QUnit.test('Test filter downloader - invalid includes', async (assert) => {
let rules;

const { FiltersDownloader } = require('../dist');
assert.ok(FiltersDownloader);

// non existing file
rules = [
'always_included_rule',
'||example.com',
'||example.org',
'!#include resources/not_found_file.txt',
];

await assert.rejects(
FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants),
`Error: Failed to resolve the include directive '!#include resources/not_found_file.txt'
Context:
always_included_rule
||example.com
||example.org
!#include resources/not_found_file.txt`,
);

// different origin
rules = [
const rules = [
'always_included_rule',
'included_rule',
'||example.org^',
Expand All @@ -735,24 +688,6 @@ Context:
];

await assert.rejects(FiltersDownloader.resolveIncludes(rules, 'http://google.com', FilterCompilerConditionsConstants));

rules = [
'always_included_rule',
'included_rule',
'||example.org^',
'||example.com^',
'!#include',
];

await assert.rejects(
FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants),
`Failed to resolve the include directive '!#include'
Context:
included_rule
||example.org^
||example.com^
!#include`,
);
});

QUnit.test('Test filter downloader - compile rules with conditional includes', async (assert) => {
Expand Down Expand Up @@ -786,7 +721,7 @@ QUnit.test('Test filter downloader - compile rules with conditional includes', a

assert.rejects(
FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants),
`Error: Failed to resolve the include directive '!#include ${URL404}'
`Failed to resolve the include directive '!#include ${URL404}'
Context:
\talways_included_rule
\t!#include ${URL404}`,
Expand Down
18 changes: 7 additions & 11 deletions src/filters-downloader-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,17 +654,13 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter
});
filter = downloadResult.filter;
} catch (error) {
if (error instanceof Error) {
const errorMessage = createErrorMessage(
'Failed to resolve the include directive',
line,
context,
filterOrigin,
);
throw new Error(errorMessage);
} else {
throw new Error(`Unknown error: ${error}`);
}
const errorMessage = createErrorMessage(
'Failed to resolve the include directive',
line,
context,
filterOrigin,
);
throw new Error(errorMessage);
}

const MAX_LINES_TO_SCAN = 50;
Expand Down

0 comments on commit eae8b8e

Please sign in to comment.