Skip to content

Commit

Permalink
AG-25861 Fix absolute path handling
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/aglint from fix/AG-25861 to master

Squashed commit of the following:

commit 882b9cb
Author: scripthunter7 <[email protected]>
Date:   Tue Jan 9 13:07:50 2024 +0100

    Prepare changelog to release

commit ca9c093
Author: scripthunter7 <[email protected]>
Date:   Tue Jan 9 11:20:02 2024 +0100

    better test case

commit 34cc587
Author: scripthunter7 <[email protected]>
Date:   Tue Jan 9 10:02:56 2024 +0100

    toThrowError is deprecated

commit de15dd6
Author: scripthunter7 <[email protected]>
Date:   Tue Jan 9 09:53:43 2024 +0100

    tests

commit d77efb3
Author: scripthunter7 <[email protected]>
Date:   Mon Jan 8 16:58:40 2024 +0100

    Update changelog

commit 33c4f4c
Author: scripthunter7 <[email protected]>
Date:   Mon Jan 8 16:26:47 2024 +0100

    Fix absolute path handling
  • Loading branch information
scripthunter7 committed Jan 9, 2024
1 parent 89f47f3 commit 64ab595
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog][keepachangelog], and this project adheres to [Semantic Versioning][semver].


## [2.0.7] - 2024-01-09

### Fixed

- Absolute paths in the CLI [#184].

## [2.0.6] - 2023-11-07

### Added
Expand Down Expand Up @@ -151,6 +157,7 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
- Initial version of the adblock rule parser.


[2.0.7]: https://github.com/AdguardTeam/AGLint/compare/v2.0.6...v2.0.7
[2.0.6]: https://github.com/AdguardTeam/AGLint/compare/v2.0.5...v2.0.6
[2.0.5]: https://github.com/AdguardTeam/AGLint/compare/v2.0.4...v2.0.5
[2.0.4]: https://github.com/AdguardTeam/AGLint/compare/v2.0.3...v2.0.4
Expand All @@ -171,4 +178,5 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
[@adguard/agtree]: https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/agtree/CHANGELOG.md

[#10]: https://github.com/AdguardTeam/AGLint/issues/10
[#184]: https://github.com/AdguardTeam/AGLint/issues/184
[#185]: https://github.com/AdguardTeam/AGLint/issues/185
12 changes: 11 additions & 1 deletion src/linter/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ export class LinterCli {
// If files are specified, use them instead of scanning the cwd
if (files.length > 0) {
for (const file of files) {
const fullPath = path.join(cwd, file);
let fullPath;

if (path.isAbsolute(file)) {
// If file path is absolute, use it as is
fullPath = file;
} else {
// Otherwise, join relative path with cwd
fullPath = path.join(cwd, file);
}

// TODO: Add support for glob patterns

// Check if the file exists
if (!(await pathExists(fullPath))) {
Expand Down
24 changes: 23 additions & 1 deletion test/linter/cli/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,30 @@ describe('CLI tests', () => {

const cli = new LinterCli(reporter);

await expect(cli.run(FIXTURE_PATH, ['dir1/dir1_file1.txt', 'dir100/dir100_file1.txt'])).rejects.toThrowError(
await expect(cli.run(FIXTURE_PATH, ['dir1/dir1_file1.txt', 'dir100/dir100_file1.txt'])).rejects.toThrow(
`File "${path.join(FIXTURE_PATH, 'dir100/dir100_file1.txt')}" does not exist`,
);
});

test('handles absolute file paths correctly', async () => {
// should work if path exists
// make 'dir1/dir1_file1.txt' absolute
const absolutePath = path.resolve(path.join(FIXTURE_PATH, 'dir1/dir1_file1.txt'));
const reporter = new TestReporter();
const cli = new LinterCli(reporter);
await cli.run(FIXTURE_PATH, [absolutePath]);
const loggedEvents = reporter.getLoggedEvents();

// there are at least 4 events (onLintStart, onFileStart, onFileEnd, onLintEnd)
// no need to check the exact events here, we already did it in the previous test,
// we just want to make sure that the absolute path works
expect(loggedEvents.length).toBeGreaterThanOrEqual(4);

// should work if path does not exist
const absolutePathNotExists = path.resolve(path.join(FIXTURE_PATH, 'dir100/dir100_file1.txt'));

await expect(cli.run(FIXTURE_PATH, [absolutePathNotExists])).rejects.toThrow(
`File "${absolutePathNotExists}" does not exist`,
);
});
});

0 comments on commit 64ab595

Please sign in to comment.