@ds-sfdc/sfparty 1.3.9 → 1.3.11

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.
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import path from 'path'
4
- import { readFile } from 'fs'
4
+ import fs from 'fs'
5
5
  import { Parser } from 'xml2js'
6
6
  import logUpdate from 'log-update'
7
7
  import clc from 'cli-color'
@@ -84,7 +84,9 @@ export class Split {
84
84
  ) {
85
85
  global.logger.error('Invalid information passed to split')
86
86
  resolve(false)
87
- } else if (!fileUtils.fileExists(that.metaFilePath)) {
87
+ } else if (
88
+ !fileUtils.fileExists({ filePath: that.metaFilePath, fs })
89
+ ) {
88
90
  global.logger.error(`file not found: ${that.metaFilePath}`)
89
91
  resolve(false)
90
92
  } else {
@@ -94,7 +96,7 @@ export class Split {
94
96
  )
95
97
  let parser = new Parser()
96
98
  const getJSON = new Promise((resolve, reject) => {
97
- readFile(that.metaFilePath, function (err, data) {
99
+ fs.readFile(that.metaFilePath, function (err, data) {
98
100
  parser.parseString(data, function (err, result) {
99
101
  if (result) {
100
102
  resolve({
@@ -0,0 +1,43 @@
1
+ import { directoryExists } from '../../../src/lib/fileUtils'
2
+
3
+ jest.mock('fs', () => {
4
+ return {
5
+ existsSync: jest.fn(),
6
+ statSync: jest.fn(),
7
+ }
8
+ })
9
+
10
+ it('should return true if the directory exists and is a directory', () => {
11
+ const fs = require('fs')
12
+ fs.existsSync.mockReturnValue(true)
13
+ fs.statSync.mockReturnValue({ isDirectory: () => true })
14
+
15
+ const dirPath = '/some/directory'
16
+ const result = directoryExists({ dirPath, fs })
17
+ expect(result).toBe(true)
18
+ expect(fs.existsSync).toHaveBeenCalledWith(dirPath)
19
+ expect(fs.statSync).toHaveBeenCalledWith(dirPath)
20
+ })
21
+
22
+ it('should return false if the directory exists but is not a directory', () => {
23
+ const fs = require('fs')
24
+ fs.existsSync.mockReturnValue(true)
25
+ fs.statSync.mockReturnValue({ isDirectory: () => false })
26
+
27
+ const dirPath = '/some/directory'
28
+ const result = directoryExists({ dirPath, fs })
29
+ expect(result).toBe(false)
30
+ expect(fs.existsSync).toHaveBeenCalledWith(dirPath)
31
+ expect(fs.statSync).toHaveBeenCalledWith(dirPath)
32
+ })
33
+
34
+ it('should return false if the directory does not exist', () => {
35
+ const fs = require('fs')
36
+ fs.existsSync.mockReturnValue(false)
37
+
38
+ const dirPath = '/some/directory'
39
+ const result = directoryExists({ dirPath, fs })
40
+ expect(result).toBe(false)
41
+ expect(fs.existsSync).toHaveBeenCalledWith(dirPath)
42
+ expect(fs.statSync).toHaveBeenCalled()
43
+ })
@@ -0,0 +1,36 @@
1
+ import fs from 'fs'
2
+ import { fileExists } from '../../../src/lib/fileUtils'
3
+
4
+ beforeEach(() => {
5
+ fs.existsSync = jest.fn()
6
+ fs.statSync = jest.fn()
7
+ })
8
+
9
+ it('should return true if file exists', () => {
10
+ fs.existsSync.mockReturnValue(true)
11
+ fs.statSync.mockReturnValue({ isFile: () => true })
12
+
13
+ const filePath = '/path/to/file.txt'
14
+ const result = fileExists({ filePath, fs })
15
+
16
+ expect(result).toBe(true)
17
+ })
18
+
19
+ it('should return false if file does not exist', () => {
20
+ fs.existsSync.mockReturnValue(false)
21
+
22
+ const filePath = '/path/to/file.txt'
23
+ const result = fileExists({ filePath, fs })
24
+
25
+ expect(result).toBe(false)
26
+ })
27
+
28
+ it('should return false if file exists but is not a file', () => {
29
+ fs.existsSync.mockReturnValue(true)
30
+ fs.statSync.mockReturnValue({ isFile: () => false })
31
+
32
+ const filePath = '/path/to/file.txt'
33
+ const result = fileExists({ filePath, fs })
34
+
35
+ expect(result).toBe(false)
36
+ })
@@ -1,52 +1,227 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync, spawn } from 'child_process'
2
2
  import { existsSync } from 'fs'
3
3
  import { diff } from '../../../src/lib/gitUtils'
4
4
 
5
5
  jest.mock('fs', () => ({
6
- existsSync: jest.fn()
6
+ existsSync: jest.fn(),
7
7
  }))
8
8
  jest.mock('child_process', () => ({
9
- execSync: jest.fn()
9
+ execSync: jest.fn(),
10
10
  }))
11
- const gitRef = "HEAD~1..HEAD"
11
+ const mockStream = {
12
+ setEncoding: jest.fn(),
13
+ on: jest.fn(),
14
+ }
15
+ jest.mock('child_process', () => ({
16
+ spawn: jest.fn(() => ({
17
+ stdout: mockStream,
18
+ stderr: mockStream,
19
+ })),
20
+ }))
21
+
22
+ const gitRef = 'HEAD~1..HEAD'
12
23
 
13
24
  beforeEach(() => {
14
- jest.clearAllMocks()
25
+ jest.clearAllMocks()
26
+ })
27
+
28
+ test('rejects if directory does not exist', async () => {
29
+ const existsSync = jest.fn().mockReturnValueOnce(false)
30
+ const spawn = jest.fn()
31
+ try {
32
+ await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
33
+ fail('Expected function to throw an error')
34
+ } catch (error) {
35
+ expect(error.message).toEqual(
36
+ 'The directory "/path/to/dir" does not exist',
37
+ )
38
+ }
15
39
  })
16
40
 
17
- test('rejects if directory is not a git repository', async () => {
18
- existsSync.mockReturnValueOnce(false)
19
- try {
20
- await diff('/path/to/dir')
21
- fail('Expected function to throw an error', gitRef, existsSync)
22
- } catch (error) {
23
- expect(error.message).toEqual('The directory "/path/to/dir" is not a git repository')
24
- }
41
+ test('rejects if .git directory does not exist', async () => {
42
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(false)
43
+
44
+ try {
45
+ await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
46
+ fail('Expected function to throw an error')
47
+ } catch (error) {
48
+ expect(error.message).toEqual(
49
+ `The directory "/path/to/dir" is not a git repository`,
50
+ )
51
+ }
52
+ })
53
+
54
+ test('rejects when git is not installed', async () => {
55
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
56
+ const git = {
57
+ on: jest.fn().mockImplementationOnce((event, callback) => {
58
+ if (event === 'error') {
59
+ callback(new Error('Command failed'))
60
+ }
61
+ }),
62
+ }
63
+ spawn.mockReturnValueOnce(git)
64
+
65
+ try {
66
+ await diff({ dir: '/path/to/dir', existsSync, spawn })
67
+ fail('Expected function to throw an error')
68
+ } catch (error) {
69
+ expect(error.message).toEqual('Git is not installed on this machine')
70
+ }
25
71
  })
26
72
 
27
73
  test('resolves with files when git diff command is successful', async () => {
28
- existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
29
- execSync.mockReturnValueOnce(
30
- `A\tfile1.txt
31
- M\tfile2.txt
32
- D\tfile3.txt`)
74
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
75
+ const git = {
76
+ on: jest.fn((event, callback) => {
77
+ if (event === 'close') {
78
+ callback(0)
79
+ }
80
+ }),
81
+ }
82
+ const gitDiff = {
83
+ stdout: {
84
+ setEncoding: jest.fn(),
85
+ on: jest.fn((event, callback) => {
86
+ if (event === 'data') {
87
+ callback('A\tfile1.txt\nM\tfile2.txt\nD\tfile3.txt')
88
+ } else if (event === 'close') {
89
+ callback(0)
90
+ }
91
+ }),
92
+ },
93
+ stderr: {
94
+ on: jest.fn((event, callback) => {
95
+ if (event === 'data') {
96
+ callback('')
97
+ }
98
+ }),
99
+ },
100
+ }
101
+ spawn.mockReturnValueOnce(git).mockReturnValueOnce(gitDiff)
102
+
103
+ const files = await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
104
+ expect(files).toEqual([
105
+ { type: 'add', path: 'file1.txt', action: 'add' },
106
+ { type: 'modify', path: 'file2.txt', action: 'add' },
107
+ { type: 'delete', path: 'file3.txt', action: 'delete' },
108
+ ])
109
+ })
110
+
111
+ test('rejects when git diff command fails', async () => {
112
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
113
+ const git = {
114
+ on: jest.fn((event, callback) => {
115
+ if (event === 'close') {
116
+ callback(0)
117
+ }
118
+ }),
119
+ }
120
+ const gitDiff = {
121
+ stderr: {
122
+ on: jest.fn(),
123
+ },
124
+ stdout: {
125
+ setEncoding: jest.fn(),
126
+ on: jest.fn(),
127
+ },
128
+ }
129
+ gitDiff.stderr.on.mockImplementation((_, cb) => cb('Command failed'))
130
+ spawn.mockReturnValueOnce(git).mockReturnValueOnce(gitDiff)
131
+
132
+ try {
133
+ await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
134
+ fail('Expected function to throw an error')
135
+ } catch (error) {
136
+ expect(error.message).toEqual(
137
+ 'git diff command failed with error: Command failed',
138
+ )
139
+ }
140
+ })
33
141
 
34
- const files = await diff('/path/to/dir', gitRef, existsSync, execSync)
35
- expect(files).toEqual([
36
- { type: 'add', path: 'file1.txt', action: 'add' },
37
- { type: 'modify', path: 'file2.txt', action: 'add' },
38
- { type: 'delete', path: 'file3.txt', action: 'delete' }
39
- ])
142
+ test('ignores files when git diff output does not have a tab character', async () => {
143
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
144
+ const git = {
145
+ on: jest.fn((event, callback) => {
146
+ if (event === 'close') {
147
+ callback(0)
148
+ }
149
+ }),
150
+ }
151
+ const gitDiff = {
152
+ stderr: {
153
+ on: jest.fn(),
154
+ },
155
+ stdout: {
156
+ setEncoding: jest.fn(),
157
+ on: jest.fn((event, callback) => {
158
+ if (event === 'data') {
159
+ callback(`\t\t\nA\tfile1.txt\nM\tfile2.txt\nfile3.txt\n`)
160
+ }
161
+ if (event === 'close') {
162
+ callback(0)
163
+ }
164
+ }),
165
+ },
166
+ }
167
+ spawn.mockReturnValueOnce(git).mockReturnValueOnce(gitDiff)
168
+
169
+ const files = await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
170
+ expect(files).toEqual([
171
+ { type: 'add', path: 'file1.txt', action: 'add' },
172
+ { type: 'modify', path: 'file2.txt', action: 'add' },
173
+ ])
174
+ })
175
+
176
+ test('rejects when git --version command fails', async () => {
177
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
178
+ const git = {
179
+ on: jest.fn((event, callback) => {
180
+ if (event === 'close') {
181
+ callback(1)
182
+ }
183
+ }),
184
+ }
185
+ spawn.mockReturnValueOnce(git)
186
+
187
+ try {
188
+ await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
189
+ fail('Expected function to throw an error')
190
+ } catch (error) {
191
+ expect(error.message).toEqual(
192
+ 'git --version command failed with code 1',
193
+ )
194
+ }
40
195
  })
41
196
 
42
197
  test('rejects when git diff command fails', async () => {
43
- existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
44
- execSync.mockImplementation(() => { throw new Error('Command failed') })
45
-
46
- try {
47
- await diff('/path/to/dir', gitRef, existsSync, execSync)
48
- fail('Expected function to throw an error')
49
- } catch (error) {
50
- expect(error.message).toEqual('Command failed')
51
- }
198
+ existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
199
+ const git = {
200
+ on: jest.fn((event, callback) => {
201
+ if (event === 'close') {
202
+ callback(0)
203
+ }
204
+ }),
205
+ }
206
+ const gitDiff = {
207
+ stderr: {
208
+ on: jest.fn(),
209
+ },
210
+ stdout: {
211
+ setEncoding: jest.fn(),
212
+ on: jest.fn((event, callback) => {
213
+ if (event === 'close') {
214
+ callback(1)
215
+ }
216
+ }),
217
+ },
218
+ }
219
+ spawn.mockReturnValueOnce(git).mockReturnValueOnce(gitDiff)
220
+
221
+ try {
222
+ await diff({ dir: '/path/to/dir', gitRef, existsSync, spawn })
223
+ fail('Expected function to throw an error')
224
+ } catch (error) {
225
+ expect(error.message).toEqual('git diff command failed with code 1')
226
+ }
52
227
  })
@@ -1,69 +1,136 @@
1
1
  import path from 'path'
2
- import { execSync } from 'child_process'
3
- import * as fileUtils from '../../../src/lib/fileUtils.js'
2
+ import fs from 'fs'
3
+ import child_process, { execSync } from 'child_process'
4
4
  import { lastCommit } from '../../../src/lib/gitUtils.js'
5
- import { existsSync } from 'fs'
6
5
 
7
- const dir = '/test/sfparty'
6
+ const dir = '/test'
8
7
  const fileName = 'index.yaml'
9
- const folder = path.resolve(dir, '.sfdx', 'sfparty')
10
- const filePath = path.resolve(folder, fileName)
11
8
 
12
- jest.mock('fs', () => ({
13
- existsSync: jest.fn()
14
- }))
9
+ const fileUtils = {
10
+ createDirectory: jest.fn(),
11
+ readFile: jest.fn((filePath) => {
12
+ if (filePath.indexOf('project') !== -1) {
13
+ return { git: { lastCommit: 'lastCommit' } }
14
+ }
15
+ }),
16
+ fileExists: jest.fn((filePath) => {
17
+ if (filePath.indexOf('project') !== -1) {
18
+ return true
19
+ }
20
+ return false
21
+ }),
22
+ }
15
23
 
16
- jest.mock('../../../src/lib/fileUtils.js', () => {
17
- return {
18
- createDirectory: jest.fn(),
19
- readFile: jest.fn()
20
- }
24
+ jest.mock('fs', () => {
25
+ return {
26
+ existsSync: jest.fn(),
27
+ statSync: jest.fn(),
28
+ }
21
29
  })
22
30
 
23
- jest.mock('child_process', () => {
24
- return {
25
- execSync: jest.fn()
26
- }
31
+ beforeEach(() => {
32
+ jest.clearAllMocks()
33
+ jest.mock('child_process', () => ({
34
+ execSync: jest.fn(() => {
35
+ return 'testCommit'
36
+ }),
37
+ }))
27
38
  })
28
39
 
29
- beforeEach(() => {
30
- jest.resetAllMocks()
40
+ test('should return lastCommit and latestCommit if file exists', async () => {
41
+ fs.existsSync.mockReturnValue(true)
42
+ const result = await lastCommit({
43
+ dir: 'project',
44
+ existsSync: fs.existsSync,
45
+ execSync: require('child_process').execSync,
46
+ fileUtils,
47
+ })
48
+ expect(result).toEqual({
49
+ lastCommit: 'lastCommit',
50
+ latestCommit: 'testCommit',
51
+ })
31
52
  })
32
53
 
33
- it('should return the last commit and latest commit', () => {
34
- const lastCommitHash = '16d69f0cf3d902a900a0609177fe5cf0fda9a961'
35
- const latestCommitHash = '16d69f0cf3d902a900a0609177fe5cf0fda9a961'
36
- fileUtils.createDirectory.mockReturnValue(undefined)
37
- fileUtils.readFile.mockReturnValue({ git: { lastCommit: lastCommitHash } })
38
- execSync.mockReturnValue(latestCommitHash)
39
- existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
40
- const result = lastCommit(dir, fileName, existsSync, execSync, fileUtils)
41
- expect(fileUtils.createDirectory).toHaveBeenCalledWith(folder)
42
- expect(fileUtils.readFile).toHaveBeenCalledWith(filePath)
43
- expect(execSync).toHaveBeenCalledWith(`git log --format=format:%H -1`, { cwd: dir, encoding: 'utf-8' })
44
- expect(result).toEqual({lastCommit: lastCommitHash, latestCommit: latestCommitHash })
54
+ test('should return only latestCommit if file does not exist', async () => {
55
+ fs.existsSync.mockReturnValue(false)
56
+ fileUtils.fileExists.mockReturnValue(false)
57
+ const result = await lastCommit({
58
+ dir: __dirname,
59
+ existsSync: fs.existsSync,
60
+ execSync: require('child_process').execSync,
61
+ fileUtils,
62
+ })
63
+ expect(result).toEqual({
64
+ lastCommit: undefined,
65
+ latestCommit: 'testCommit',
66
+ })
45
67
  })
46
68
 
47
- it('should return the latest commit if no lastCommit is found', () => {
48
- const latestCommit = '16d69f0cf3d902a900a0609177fe5cf0fda9a961'
49
- fileUtils.createDirectory.mockReturnValue(undefined)
50
- fileUtils.readFile.mockReturnValue({ git: {} })
51
- execSync.mockReturnValue(latestCommit)
52
- existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
53
- const result = lastCommit(dir, fileName, existsSync, execSync, fileUtils)
54
- expect(fileUtils.createDirectory).toHaveBeenCalledWith(folder)
55
- expect(fileUtils.readFile).toHaveBeenCalledWith(filePath)
56
- expect(execSync).toHaveBeenCalledWith(`git log --format=format:%H -1`, { cwd: dir, encoding: 'utf-8' })
57
- expect(result).toEqual({ lastCommit: undefined, latestCommit })
69
+ it('should throw an error', async () => {
70
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => false)
71
+ try {
72
+ await lastCommit({
73
+ dir,
74
+ fileName,
75
+ existsSync: fs.existsSync,
76
+ execSync: require('child_process').execSync,
77
+ fileUtils,
78
+ })
79
+ } catch (e) {
80
+ expect(e.message).toBe(
81
+ `ENOENT: no such file or directory, access '${path.join(
82
+ dir,
83
+ '.sfdx',
84
+ 'sfparty',
85
+ fileName,
86
+ )}'`,
87
+ )
88
+ }
58
89
  })
59
90
 
60
- it('should throw an error when fileUtils.readFile is called', () => {
61
- const errorMessage = 'Unable to read file'
62
- fileUtils.createDirectory.mockReturnValue(undefined)
63
- fileUtils.readFile.mockImplementation(() => { throw new Error(errorMessage) })
64
- execSync.mockReturnValue(undefined)
65
- existsSync.mockReturnValueOnce(true).mockReturnValueOnce(true)
66
- expect(() => lastCommit(dir, fileName, existsSync, execSync, fileUtils)).toThrowError(errorMessage)
67
- expect(fileUtils.createDirectory).toHaveBeenCalledWith(folder)
68
- expect(fileUtils.readFile).toHaveBeenCalledWith(filePath)
91
+ it('should return only latest commit if lastCommit is undefined', async () => {
92
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => true)
93
+ jest.spyOn(fileUtils, 'readFile').mockImplementation(() => ({ git: {} }))
94
+ jest.spyOn(child_process, 'execSync').mockImplementation(
95
+ () => 'latestCommit',
96
+ )
97
+
98
+ const result = await lastCommit({
99
+ dir: '/test',
100
+ fileUtils,
101
+ fs,
102
+ existsSync: fs.existsSync,
103
+ execSync: child_process.execSync,
104
+ })
105
+
106
+ expect(result).toEqual({
107
+ lastCommit: undefined,
108
+ latestCommit: 'latestCommit',
109
+ })
110
+ expect(fs.existsSync).toHaveBeenCalledWith('/test/.sfdx/sfparty/index.yaml')
111
+ expect(fileUtils.readFile).toHaveBeenCalledWith(
112
+ '/test/.sfdx/sfparty/index.yaml',
113
+ )
114
+ expect(child_process.execSync).toHaveBeenCalledWith(
115
+ 'git log --format=format:%H -1',
116
+ { cwd: '/test', encoding: 'utf-8' },
117
+ )
118
+ })
119
+
120
+ test('should throw an error when execSync returns an error', async () => {
121
+ jest.spyOn(child_process, 'execSync').mockImplementation(() => {
122
+ throw new Error('execSync error')
123
+ })
124
+
125
+ try {
126
+ await lastCommit({
127
+ dir: '/test',
128
+ fileUtils,
129
+ fs,
130
+ existsSync: fs.existsSync,
131
+ execSync: child_process.execSync,
132
+ })
133
+ } catch (e) {
134
+ expect(e.message).toBe('execSync error')
135
+ }
69
136
  })