Skip to content

Commit bbdf55f

Browse files
committed
separate cwd and workspace getter, git add requires workspace to filter files, blob works with current directory that node.js is running at
1 parent 4608134 commit bbdf55f

5 files changed

Lines changed: 46 additions & 31 deletions

File tree

__tests__/git.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
describe('Git CLI', () => {
1313
beforeEach(() => {
1414
jest.clearAllMocks()
15-
jest.spyOn(cwd, 'getCwd').mockReturnValue('/users/test-workspace')
1615
})
1716

1817
describe('git checkout', () => {
@@ -105,19 +104,18 @@ describe('Git CLI', () => {
105104
})
106105

107106
describe('git add', () => {
107+
beforeEach(() => {
108+
jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace')
109+
})
110+
108111
it('should ensure file paths are within curent working directory', async () => {
109112
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
110113

111114
await addFileChanges(['*.ts', '~/.bashrc'])
112115
expect(execMock).toHaveBeenCalled()
113116
expect(execMock).toHaveBeenCalledWith(
114117
'git',
115-
[
116-
'add',
117-
'--',
118-
'/users/test-workspace/*.ts',
119-
'/users/test-workspace/~/.bashrc',
120-
],
118+
['add', '--', '/test-workspace/*.ts', '/test-workspace/~/.bashrc'],
121119
expect.objectContaining({
122120
listeners: { stdline: expect.anything(), errline: expect.anything() },
123121
})

__tests__/utils/cwd.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
it,
88
jest,
99
} from '@jest/globals'
10-
import { getCwd } from '../../src/utils/cwd'
10+
import { getCwd, getWorkspace } from '../../src/utils/cwd'
1111

1212
describe('Current Working Directory', () => {
1313
beforeEach(() => {
@@ -16,6 +16,21 @@ describe('Current Working Directory', () => {
1616
})
1717

1818
describe('getCwd', () => {
19+
it('should read from process.cwd', async () => {
20+
jest.spyOn(process, 'cwd').mockReturnValue('/my-process-cwd')
21+
22+
expect(getCwd()).toBe('/my-process-cwd')
23+
})
24+
})
25+
})
26+
27+
describe('Current Workspace', () => {
28+
beforeEach(() => {
29+
jest.clearAllMocks()
30+
jest.spyOn(core, 'debug').mockReturnValue()
31+
})
32+
33+
describe('getWorkspace', () => {
1934
let replacedEnv: jest.Replaced<typeof process.env> | undefined
2035

2136
afterEach(() => {
@@ -27,7 +42,7 @@ describe('Current Working Directory', () => {
2742
GITHUB_WORKSPACE: '/users/test',
2843
})
2944

30-
expect(getCwd()).toBe('/users/test')
45+
expect(getWorkspace()).toBe('/users/test')
3146
})
3247

3348
it('should read from workspace input if any', async () => {
@@ -38,7 +53,7 @@ describe('Current Working Directory', () => {
3853
return ''
3954
})
4055

41-
expect(getCwd()).toBe('/users/my-workspace')
56+
expect(getWorkspace()).toBe('/users/my-workspace')
4257
expect(getInputMock).toHaveBeenCalled()
4358
})
4459
})

dist/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30545,11 +30545,11 @@ function pushCurrentBranch() {
3054530545
}
3054630546
function addFileChanges(globPatterns) {
3054730547
return __awaiter(this, void 0, void 0, function* () {
30548-
const cwd = (0, cwd_1.getCwd)();
30549-
const cwdPaths = globPatterns.map((p) => (0, node_path_1.join)(cwd, p));
30548+
const workspace = (0, cwd_1.getWorkspace)();
30549+
const workspacePaths = globPatterns.map((p) => (0, node_path_1.join)(workspace, p));
3055030550
const debugOutput = [];
3055130551
const warningOutput = [];
30552-
yield (0, exec_1.exec)('git', ['add', '--', ...cwdPaths], {
30552+
yield (0, exec_1.exec)('git', ['add', '--', ...workspacePaths], {
3055330553
silent: true,
3055430554
ignoreReturnCode: true,
3055530555
listeners: {
@@ -30999,20 +30999,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
3099930999
};
3100031000
Object.defineProperty(exports, "__esModule", ({ value: true }));
3100131001
exports.getCwd = getCwd;
31002+
exports.getWorkspace = getWorkspace;
3100231003
const core = __importStar(__nccwpck_require__(2186));
3100331004
const input_1 = __nccwpck_require__(5073);
3100431005
function getCwd() {
31005-
const workspace = (0, input_1.getInput)('workspace', {
31006-
default: process.env.GITHUB_WORKSPACE,
31007-
});
31008-
if (workspace) {
31009-
core.debug(`cwd: ${workspace}`);
31010-
return workspace;
31011-
}
3101231006
const current = process.cwd();
3101331007
core.debug(`cwd: ${current}`);
3101431008
return current;
3101531009
}
31010+
function getWorkspace() {
31011+
const workspace = (0, input_1.getInput)('workspace', {
31012+
default: process.env.GITHUB_WORKSPACE,
31013+
});
31014+
core.debug(`workspace: ${workspace}`);
31015+
return workspace;
31016+
}
3101631017

3101731018

3101831019
/***/ }),

src/git.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
FileDeletion,
88
} from '@octokit/graphql-schema'
99

10-
import { getCwd } from './utils/cwd'
10+
import { getWorkspace } from './utils/cwd'
1111

1212
export async function switchBranch(branch: string) {
1313
const debugOutput: string[] = []
@@ -57,12 +57,12 @@ export async function pushCurrentBranch() {
5757
}
5858

5959
export async function addFileChanges(globPatterns: string[]): Promise<void> {
60-
const cwd = getCwd()
61-
const cwdPaths = globPatterns.map((p) => join(cwd, p))
60+
const workspace = getWorkspace()
61+
const workspacePaths = globPatterns.map((p) => join(workspace, p))
6262

6363
const debugOutput: string[] = []
6464
const warningOutput: string[] = []
65-
await exec('git', ['add', '--', ...cwdPaths], {
65+
await exec('git', ['add', '--', ...workspacePaths], {
6666
silent: true,
6767
ignoreReturnCode: true,
6868
listeners: {

src/utils/cwd.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import * as core from '@actions/core'
22
import { getInput } from './input'
33

44
export function getCwd() {
5-
const workspace = getInput('workspace', {
6-
default: process.env.GITHUB_WORKSPACE,
7-
})
8-
if (workspace) {
9-
core.debug(`cwd: ${workspace}`)
10-
return workspace
11-
}
125
const current = process.cwd()
136
core.debug(`cwd: ${current}`)
147
return current
158
}
9+
10+
export function getWorkspace() {
11+
const workspace = getInput('workspace', {
12+
default: process.env.GITHUB_WORKSPACE,
13+
})
14+
core.debug(`workspace: ${workspace}`)
15+
return workspace
16+
}

0 commit comments

Comments
 (0)