diff --git a/CHANGELOG.md b/CHANGELOG.md index 97cdd41..1b08fa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 @@ -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 diff --git a/src/linter/cli/index.ts b/src/linter/cli/index.ts index ef3d5da..2e916f4 100644 --- a/src/linter/cli/index.ts +++ b/src/linter/cli/index.ts @@ -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))) { diff --git a/test/linter/cli/cli.test.ts b/test/linter/cli/cli.test.ts index 89e0357..dd05ed8 100644 --- a/test/linter/cli/cli.test.ts +++ b/test/linter/cli/cli.test.ts @@ -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`, + ); + }); });