Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions dev-packages/cli/src/commands/check-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ async function getFiles(rootDir: string, options: HeaderCheckOptions): Promise<s
return resolveFiles(result);
}

let changedFiles = options.type === 'changes' ? getChangesComparedToDefaultBranch(rootDir) : getChangesOfLastCommit(rootDir);
changedFiles = changedFiles.filter(minimatch.filter(includePattern));
const changedFiles = options.type === 'changes' ? getChangesComparedToDefaultBranch(rootDir) : getChangesOfLastCommit(rootDir);
// The change-detection helpers return absolute paths, but the include/exclude globs are
// repo-relative -> convert
let relativeFiles = changedFiles.map(file => path.relative(rootDir, file)).filter(minimatch.filter(includePattern));

excludePattern.forEach(pattern => {
changedFiles = changedFiles.filter(minimatch.filter(`!${pattern}`));
relativeFiles = relativeFiles.filter(minimatch.filter(`!${pattern}`));
});
const result = changedFiles.filter(file => fs.existsSync(file));
const result = relativeFiles.filter(file => fs.existsSync(path.resolve(rootDir, file)));
return resolveFiles(result);
}

Expand Down
37 changes: 34 additions & 3 deletions dev-packages/cli/tests/e2e/check-header.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { describe, it, beforeEach, afterEach, expect } from 'vitest';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { runCli } from '../helpers/cli-helper';
import { commitFile, createTempGitRepo } from '../helpers/git-helper';
import { cleanupTempDir } from '../helpers/test-helper';
import { commitFile, createTempGitRepo, initGitRepo } from '../helpers/git-helper';
import { cleanupTempDir, createTempDir } from '../helpers/test-helper';

const VALID_HEADER = `/********************************************************************************
* Copyright (c) ${new Date().getFullYear()} EclipseSource and others.
Expand Down Expand Up @@ -108,6 +108,37 @@ describe('checkHeaders e2e', () => {
expect(result.stdout).to.contain('Check copy right headers of 1 files');
});

describe('with a repository under a dot-directory', () => {
let hiddenRepoDir: string;
let hiddenRoot: string;

beforeEach(() => {
hiddenRoot = createTempDir();
hiddenRepoDir = initGitRepo(path.join(hiddenRoot, '.hidden', 'repo'));
});

afterEach(() => {
cleanupTempDir(hiddenRoot);
});

it('should still detect changed files with --type changes', () => {
commitFile(hiddenRepoDir, 'src/base.ts', VALID_HEADER + '\nconst x = 1;\n', 'add base file');
execSync('git checkout -b feature', { cwd: hiddenRepoDir });
commitFile(hiddenRepoDir, 'src/feature.ts', 'const y = 2;\n', 'add feature file');
const result = runCli(['checkHeaders', hiddenRepoDir, '--type', 'changes']);
expect(result.stdout).to.contain('Check copy right headers of 1 files');
expect(result.stdout).to.contain('feature.ts');
expect(result.stdout).to.not.contain('base.ts');
});

it('should still detect changed files with --type lastCommit', () => {
commitFile(hiddenRepoDir, 'src/old.ts', VALID_HEADER + '\nconst x = 1;\n', 'add old file');
commitFile(hiddenRepoDir, 'src/new.ts', 'const y = 2;\n', 'add new file');
const result = runCli(['checkHeaders', hiddenRepoDir, '--type', 'lastCommit']);
expect(result.stdout).to.contain('Check copy right headers of 1 files');
});
});

it('should check custom file extensions with --fileExtensions', () => {
commitFile(repoDir, 'src/script.js', 'const x = 1;\n', 'add js file');
commitFile(repoDir, 'src/good.ts', VALID_HEADER + '\nconst y = 2;\n', 'add ts file');
Expand Down
7 changes: 6 additions & 1 deletion dev-packages/cli/tests/helpers/git-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ function git(args: string, cwd: string): string {

/** Creates a temporary git repository with an initial commit. */
export function createTempGitRepo(): string {
const dir = createTempDir();
return initGitRepo(createTempDir());
}

/** Initializes a git repository with an initial commit at the given directory (created if missing). */
export function initGitRepo(dir: string): string {
fs.mkdirSync(dir, { recursive: true });
git('init', dir);
git('config user.email "test@test.com"', dir);
git('config user.name "Test"', dir);
Expand Down