@ds-sfdc/sfparty 1.4.16 → 1.4.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ds-sfdc/sfparty",
3
- "version": "1.4.16",
3
+ "version": "1.4.17",
4
4
  "description": "Salesforce metadata XML splitter for CI/CD",
5
5
  "type": "module",
6
6
  "repository": {
@@ -1,5 +1,6 @@
1
1
  import path from 'path'
2
2
  import * as os from 'node:os'
3
+ import { execSync } from 'child_process'
3
4
 
4
5
  const defaultDefinition = {
5
6
  git: {
@@ -187,10 +188,12 @@ export function updateLastCommit({ dir, latest, fileUtils, fs }) {
187
188
  throw new Error(
188
189
  `updateLastCommit received a ${typeof latest} instead of string`,
189
190
  )
191
+
190
192
  if (latest !== undefined) {
191
193
  const folder = path.join(dir, '.sfdx', 'sfparty')
192
194
  const fileName = path.join(folder, 'index.yaml')
193
195
  let data = undefined
196
+
194
197
  if (fileUtils.fileExists({ filePath: fileName, fs })) {
195
198
  data = fileUtils.readFile(fileName)
196
199
  }
@@ -199,7 +202,20 @@ export function updateLastCommit({ dir, latest, fileUtils, fs }) {
199
202
  data = defaultDefinition
200
203
  }
201
204
 
202
- data.git.lastCommit = latest
205
+ // Determine the current branch name
206
+ const currentBranch = execSync(`git rev-parse --abbrev-ref HEAD`, {
207
+ cwd: dir,
208
+ encoding: 'utf-8',
209
+ }).trim()
210
+
211
+ // Initialize branches object if not exist
212
+ if (!data.git.branches) {
213
+ data.git.branches = {}
214
+ }
215
+
216
+ // Update the last commit for the current branch
217
+ data.git.branches[currentBranch] = latest
218
+
203
219
  fileUtils.saveFile(data, fileName)
204
220
  }
205
221
  }
@@ -1,6 +1,6 @@
1
- import path from 'path'
2
1
  import * as fileUtils from '../../../src/lib/fileUtils.js'
3
2
  import { updateLastCommit } from '../../../src/lib/gitUtils.js'
3
+ import { execSync } from 'child_process'
4
4
 
5
5
  let dir, latest
6
6
 
@@ -33,41 +33,37 @@ it('should not update lastCommit property if latest is undefined', () => {
33
33
  expect(fileUtils.saveFile).not.toHaveBeenCalled()
34
34
  })
35
35
 
36
- it('should update lastCommit property in index.yaml file', () => {
37
- // mock the fileExists method of fileUtils
36
+ jest.mock('child_process', () => ({
37
+ ...jest.requireActual('child_process'), // This line ensures that other child_process methods are still available as normal
38
+ execSync: jest.fn().mockReturnValue('mock-branch'),
39
+ }))
40
+
41
+ // Then in your test:
42
+ it('should update lastCommit property in index.yaml for the current branch', () => {
38
43
  fileUtils.fileExists = jest.fn(() => true)
39
- // mock the readFile method of fileUtils
40
44
  fileUtils.readFile = jest.fn(() => ({
41
- git: { lastCommit: '1111111111abcdef' },
45
+ git: { branches: { 'mock-branch': '1111111111abcdef' } },
42
46
  }))
43
- // mock the saveFile method of fileUtils
44
47
  fileUtils.saveFile = jest.fn()
48
+
45
49
  updateLastCommit({ dir, latest, fileUtils })
46
- expect(fileUtils.readFile).toHaveBeenCalled()
50
+
47
51
  expect(fileUtils.saveFile).toHaveBeenCalledWith(
48
- { git: { lastCommit: latest } },
52
+ { git: { branches: { 'mock-branch': latest } } },
49
53
  '/test/directory/.sfdx/sfparty/index.yaml',
50
54
  )
51
55
  })
52
56
 
53
- it('should use existing index.yaml if it exists', () => {
54
- fileUtils.fileExists = jest.fn(() => false)
55
- fileUtils.readFile = jest.fn()
56
- fileUtils.saveFile = jest.fn()
57
- updateLastCommit({ dir, latest, fileUtils })
58
- expect(fileUtils.fileExists).toHaveBeenCalled()
59
- expect(fileUtils.readFile).not.toHaveBeenCalled()
60
- expect(fileUtils.saveFile).toHaveBeenCalled()
61
- })
62
-
63
- it('should save the default definition if file does not exist', () => {
57
+ it('should save the default definition with branches object if file does not exist', () => {
58
+ jest.mock('child_process', () => ({
59
+ execSync: jest.fn().mockReturnValue('mock-branch'),
60
+ }))
64
61
  fileUtils.fileExists = jest.fn(() => false)
65
- fileUtils.readFile = jest.fn()
66
62
  fileUtils.saveFile = jest.fn()
67
63
 
68
- const defaultDefinition = {
64
+ const defaultDefinitionWithBranches = {
69
65
  git: {
70
- lastCommit: latest,
66
+ branches: { 'mock-branch': latest },
71
67
  },
72
68
  local: {
73
69
  lastDate: undefined,
@@ -75,10 +71,9 @@ it('should save the default definition if file does not exist', () => {
75
71
  }
76
72
 
77
73
  updateLastCommit({ dir, latest, fileUtils })
78
- expect(fileUtils.fileExists).toHaveBeenCalled()
79
- expect(fileUtils.readFile).not.toHaveBeenCalled()
74
+
80
75
  expect(fileUtils.saveFile).toHaveBeenCalledWith(
81
- defaultDefinition,
76
+ defaultDefinitionWithBranches,
82
77
  '/test/directory/.sfdx/sfparty/index.yaml',
83
78
  )
84
79
  })