Skip to content
Open
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
67 changes: 37 additions & 30 deletions __tests__/branch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,55 @@ import {
toBranchMatchConfig,
BranchMatchConfig
} from '../src/branch';
import * as github from '@actions/github';

jest.mock('@actions/core');
jest.mock('@actions/github');

describe('getBranchName', () => {
const prData = {
base: {
ref: 'base-branch-name'
},
head: {
ref: 'head-branch-name'
}
};

describe('when the pull requests base branch is requested', () => {
it('returns the base branch name', () => {
const result = getBranchName('base');
const result = getBranchName(prData, 'base');
expect(result).toEqual('base-branch-name');
});
});

describe('when the pull requests head branch is requested', () => {
it('returns the head branch name', () => {
const result = getBranchName('head');
const result = getBranchName(prData, 'head');
expect(result).toEqual('head-branch-name');
});
});
});

describe('checkAllBranch', () => {
beforeEach(() => {
github.context.payload.pull_request!.head = {
ref: 'test/feature/123'
};
github.context.payload.pull_request!.base = {
const prData = {
base: {
ref: 'main'
};
});
},
head: {
ref: 'test/feature/123'
}
};

describe('when a single pattern is provided', () => {
describe('and the pattern matches the head branch', () => {
it('returns true', () => {
const result = checkAllBranch(['^test'], 'head');
const result = checkAllBranch(prData, ['^test'], 'head');
expect(result).toBe(true);
});
});

describe('and the pattern does not match the head branch', () => {
it('returns false', () => {
const result = checkAllBranch(['^feature/'], 'head');
const result = checkAllBranch(prData, ['^feature/'], 'head');
expect(result).toBe(false);
});
});
Expand All @@ -55,21 +62,21 @@ describe('checkAllBranch', () => {
describe('when multiple patterns are provided', () => {
describe('and not all patterns matched', () => {
it('returns false', () => {
const result = checkAllBranch(['^test/', '^feature/'], 'head');
const result = checkAllBranch(prData, ['^test/', '^feature/'], 'head');
expect(result).toBe(false);
});
});

describe('and all patterns match', () => {
it('returns true', () => {
const result = checkAllBranch(['^test/', '/feature/'], 'head');
const result = checkAllBranch(prData, ['^test/', '/feature/'], 'head');
expect(result).toBe(true);
});
});

describe('and no patterns match', () => {
it('returns false', () => {
const result = checkAllBranch(['^feature/', '/test$'], 'head');
const result = checkAllBranch(prData, ['^feature/', '/test$'], 'head');
expect(result).toBe(false);
});
});
Expand All @@ -78,34 +85,34 @@ describe('checkAllBranch', () => {
describe('when the branch to check is specified as the base branch', () => {
describe('and the pattern matches the base branch', () => {
it('returns true', () => {
const result = checkAllBranch(['^main$'], 'base');
const result = checkAllBranch(prData, ['^main$'], 'base');
expect(result).toBe(true);
});
});
});
});

describe('checkAnyBranch', () => {
beforeEach(() => {
github.context.payload.pull_request!.head = {
ref: 'test/feature/123'
};
github.context.payload.pull_request!.base = {
const prData = {
base: {
ref: 'main'
};
});
},
head: {
ref: 'test/feature/123'
}
};

describe('when a single pattern is provided', () => {
describe('and the pattern matches the head branch', () => {
it('returns true', () => {
const result = checkAnyBranch(['^test'], 'head');
const result = checkAnyBranch(prData, ['^test'], 'head');
expect(result).toBe(true);
});
});

describe('and the pattern does not match the head branch', () => {
it('returns false', () => {
const result = checkAnyBranch(['^feature/'], 'head');
const result = checkAnyBranch(prData, ['^feature/'], 'head');
expect(result).toBe(false);
});
});
Expand All @@ -114,21 +121,21 @@ describe('checkAnyBranch', () => {
describe('when multiple patterns are provided', () => {
describe('and at least one pattern matches', () => {
it('returns true', () => {
const result = checkAnyBranch(['^test/', '^feature/'], 'head');
const result = checkAnyBranch(prData, ['^test/', '^feature/'], 'head');
expect(result).toBe(true);
});
});

describe('and all patterns match', () => {
it('returns true', () => {
const result = checkAnyBranch(['^test/', '/feature/'], 'head');
const result = checkAnyBranch(prData, ['^test/', '/feature/'], 'head');
expect(result).toBe(true);
});
});

describe('and no patterns match', () => {
it('returns false', () => {
const result = checkAnyBranch(['^feature/', '/test$'], 'head');
const result = checkAnyBranch(prData, ['^feature/', '/test$'], 'head');
expect(result).toBe(false);
});
});
Expand All @@ -137,7 +144,7 @@ describe('checkAnyBranch', () => {
describe('when the branch to check is specified as the base branch', () => {
describe('and the pattern matches the base branch', () => {
it('returns true', () => {
const result = checkAnyBranch(['^main$'], 'base');
const result = checkAnyBranch(prData, ['^main$'], 'base');
expect(result).toBe(true);
});
});
Expand Down
23 changes: 16 additions & 7 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,30 @@ describe('toMatchConfig', () => {
});

describe('checkMatchConfigs', () => {
const prData = {
base: {
ref: 'base-branch-name'
},
head: {
ref: 'head-branch-name'
}
};

describe('when a single match config is provided', () => {
const matchConfig: MatchConfig[] = [
{any: [{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]}]}
];

it('returns true when our pattern does match changed files', () => {
const changedFiles = ['foo.txt', 'bar.txt'];
const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);

expect(result).toBeTruthy();
});

it('returns false when our pattern does not match changed files', () => {
const changedFiles = ['foo.docx'];
const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);

expect(result).toBeFalsy();
});
Expand All @@ -118,20 +127,20 @@ describe('checkMatchConfigs', () => {
];
const changedFiles = ['foo.txt', 'bar.txt'];

const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);
expect(result).toBe(true);
});

it('returns false for a file starting with dot if `dot` option is false', () => {
const changedFiles = ['.foo.txt'];
const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);

expect(result).toBeFalsy();
});

it('returns true for a file starting with dot if `dot` option is true', () => {
const changedFiles = ['.foo.txt'];
const result = checkMatchConfigs(changedFiles, matchConfig, true);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, true);

expect(result).toBeTruthy();
});
Expand All @@ -145,7 +154,7 @@ describe('checkMatchConfigs', () => {
const changedFiles = ['foo.txt', 'bar.md'];

it('returns false when only one config matches', () => {
const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);
expect(result).toBe(false);
});

Expand All @@ -154,7 +163,7 @@ describe('checkMatchConfigs', () => {
{any: [{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]}]},
{any: [{headBranch: ['head-branch']}]}
];
const result = checkMatchConfigs(changedFiles, matchConfig, false);
const result = checkMatchConfigs(prData, changedFiles, matchConfig, false);
expect(result).toBe(true);
});
});
Expand Down
Loading