Skip to content

Commit

Permalink
fix: override forceignore defaults (#1224)
Browse files Browse the repository at this point in the history
* fix: user contents go AFTER defaults to allow overriding

* fix: remove windows backslash support but keep warning

* test: ut for ignoring dot files via overriding the default ignores

* test: remove redundant tests about defaults
  • Loading branch information
mshanemc authored Jan 31, 2024
1 parent 3790598 commit 954097b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
15 changes: 6 additions & 9 deletions src/resolve/forceIgnore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ export class ForceIgnore {

public constructor(forceIgnorePath = '') {
try {
let contents = readFileSync(forceIgnorePath, 'utf-8');
const contents = readFileSync(forceIgnorePath, 'utf-8');
// check if file `.forceignore` exists
if (contents !== undefined) {
// check for windows style separators (\) and warn
if (contents.includes('\\')) {
const lifecycle = Lifecycle.getInstance();
// cannot await a method in a constructor
void lifecycle.emitWarning(
'Your .forceignore file incorrectly uses the backslash ("\\") as a folder separator; it should use the slash ("/") instead. We currently accept both separators, but we plan to stop supporting the backslash soon.'
// void because you cannot await a method in a constructor
void Lifecycle.getInstance().emitWarning(
'Your .forceignore file incorrectly uses the backslash ("\\") as a folder separator; it should use the slash ("/") instead. The ignore rules will not work as expected until you fix this.'
);
// TODO: change this in v56 to only emit warning but NOT fix file
contents = contents.replace(/\\/g, '/');
}

// add the default ignore paths, and then parse the .forceignore file
this.parser = ignore().add(`${contents}\n${this.DEFAULT_IGNORE.join('\n')}`);
this.parser = ignore().add(`${this.DEFAULT_IGNORE.join('\n')}\n${contents}`);

this.forceIgnoreDirectory = dirname(forceIgnorePath);
}
} catch (e) {
Expand Down
20 changes: 15 additions & 5 deletions test/resolve/forceIgnore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { expect } from 'chai';
import { createSandbox } from 'sinon';
import * as fs from 'graceful-fs';
import { Lifecycle } from '@salesforce/core';
import { ForceIgnore } from '../../src';
import { ForceIgnore } from '../../src/resolve/forceIgnore';
import * as fsUtil from '../../src/utils/fileSystemHandler';

const env = createSandbox();
Expand All @@ -29,22 +29,21 @@ describe('ForceIgnore', () => {
});

it('Should ignore files with a given pattern', () => {
const readStub = env.stub(fs, 'readFileSync');
readStub.withArgs(forceIgnorePath).returns(testPattern);
env.stub(fs, 'readFileSync').returns(testPattern);
const forceIgnore = new ForceIgnore(forceIgnorePath);
expect(forceIgnore.accepts(testPath)).to.be.false;
expect(forceIgnore.denies(testPath)).to.be.true;
});

it('Should ignore files with windows separators', () => {
it('windows separators no longer have any effect', () => {
const lifecycleStub = env.stub(Lifecycle.prototype, 'emitWarning');
const forceIgnoreEntry = 'force-app\\main\\default\\classes\\myApex.*';
const pathToClass = join('force-app', 'main', 'default', 'classes', 'myApex.cls');
env.stub(fs, 'readFileSync').returns(forceIgnoreEntry);

const forceIgnore = new ForceIgnore();

expect(forceIgnore.accepts(pathToClass)).to.be.false;
expect(forceIgnore.accepts(pathToClass)).to.be.true;
expect(lifecycleStub.callCount).to.equal(1);
});

Expand Down Expand Up @@ -110,5 +109,16 @@ describe('ForceIgnore', () => {
expect(forceIgnore.accepts(manifestPath)).to.be.false;
expect(forceIgnore.denies(manifestPath)).to.be.true;
});

it('Should allow .forceignore file to override defaults', () => {
// tamper with the file
env.restore();
env.stub(fs, 'readFileSync').returns('!**/.*');
forceIgnore = new ForceIgnore();

const dotFilePath = join(root, '.foo');
expect(forceIgnore.accepts(dotFilePath)).to.be.true;
expect(forceIgnore.denies(dotFilePath)).to.be.false;
});
});
});

0 comments on commit 954097b

Please sign in to comment.