Skip to content

Commit

Permalink
feat: process lines related to metadata entity only (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Apr 4, 2023
1 parent 2e8f9fb commit f4c5483
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 80 deletions.
21 changes: 21 additions & 0 deletions __tests__/unit/lib/service/inFolderHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,25 @@ describe('InFolderHander', () => {
})
})
})
describe('when the line should not be processed', () => {
it.each([`force-app/main/default/${objectType}/test.otherExtension`])(
'does not handle the line',
async entityPath => {
// Arrange
const sut = new InFolder(
`A ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(copyFiles).not.toHaveBeenCalled()
}
)
})
})
47 changes: 36 additions & 11 deletions __tests__/unit/lib/service/inResourceHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ jest.mock('fs-extra')
jest.mock('fs')

const objectType = 'staticresources'
const entityPath = 'force-app/main/default/staticresources/resource.js'
const element = 'myResources'
const basePath = 'force-app/main/default/staticresources'
const entityPath = `${basePath}/${element}.js`
const line = `A ${entityPath}`
const type = 'resource'
let work
beforeEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -48,7 +51,7 @@ describe('InResourceHandler', () => {
await sut.handle()

// Assert
expect(...work.diffs.package.get(objectType)).toEqual('resource')
expect(...work.diffs.package.get(objectType)).toEqual(element)
expect(copyFiles).not.toBeCalled()
})
})
Expand Down Expand Up @@ -133,7 +136,7 @@ describe('InResourceHandler', () => {
})

describe('when no matching resource exist', () => {
it('should not copy any files', async () => {
it('should not copy resource files', async () => {
// Arrange
const sut = new InResourceHandler(
line,
Expand All @@ -147,12 +150,12 @@ describe('InResourceHandler', () => {
await sut.handle()

// Assert
expect(...work.diffs.package.get(objectType)).toEqual('resource')
expect(...work.diffs.package.get(objectType)).toEqual(element)
expect(copyFiles).toBeCalledTimes(2)
expect(copyFiles).toHaveBeenCalledWith(work.config, `${entityPath}`)
expect(copyFiles).toHaveBeenCalledWith(
expect(copyFiles).not.toHaveBeenCalledWith(
work.config,
`${entityPath}${METAFILE_SUFFIX}`
`${basePath}/${element}.${type}${METAFILE_SUFFIX}`
)
})
})
Expand All @@ -165,7 +168,7 @@ describe('InResourceHandler', () => {
})
describe('When only a ressource sub element is deleted', () => {
beforeEach(() => {
pathExists.mockImplementationOnce(() => true)
pathExists.mockResolvedValue(true)
})
it('should treat it as a modification', async () => {
// Arrange
Expand All @@ -180,7 +183,7 @@ describe('InResourceHandler', () => {
await sut.handle()

// Assert
expect(...work.diffs.package.get(objectType)).toEqual('resource')
expect(...work.diffs.package.get(objectType)).toEqual(element)
expect(pathExists).toHaveBeenCalledWith(
expect.stringContaining('resource'),
work.config
Expand All @@ -189,7 +192,7 @@ describe('InResourceHandler', () => {
})
describe('When the ressource is deleted', () => {
beforeEach(() => {
pathExists.mockImplementationOnce(() => false)
pathExists.mockResolvedValue(false)
})
it('should treat it as a deletion', async () => {
// Arrange
Expand All @@ -206,13 +209,35 @@ describe('InResourceHandler', () => {
// Assert
expect(work.diffs.package.has(objectType)).toBe(false)
expect(...work.diffs.destructiveChanges.get(objectType)).toEqual(
'resource'
element
)
expect(pathExists).toHaveBeenCalledWith(
expect.stringContaining('resource'),
expect.stringContaining(element),
work.config
)
})
})
})

describe('when the line should not be processed', () => {
it.each([`${basePath}/.eslintrc.json`])(
'does not handle the line',
async entityPath => {
// Arrange
const sut = new InResourceHandler(
`A ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(copyFiles).not.toHaveBeenCalled()
}
)
})
})
22 changes: 22 additions & 0 deletions __tests__/unit/lib/service/standardHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,26 @@ describe(`StandardHandler`, () => {
}
)
})

describe('when the line should not be processed', () => {
it.each([`force-app/main/default/classes/folder/Random.file`])(
'does not handle the line',
async entityPath => {
// Arrange
const sut = new StandardHandler(
`A ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(copyFiles).not.toHaveBeenCalled()
}
)
})
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@oclif/command": "^1.8.22",
"@oclif/config": "^1.18.8",
"@oclif/errors": "^1.3.6",
"@salesforce/command": "^5.3.4",
"@salesforce/command": "^5.3.5",
"@salesforce/core": "^3.34.6",
"fast-xml-parser": "^4.1.3",
"fs-extra": "^11.1.1",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@commitlint/config-conventional": "^17.4.4",
"@commitlint/prompt-cli": "^17.5.0",
"@oclif/dev-cli": "^1.26.10",
"@oclif/plugin-help": "^5.2.8",
"@oclif/plugin-help": "^5.2.9",
"@oclif/test": "^2.3.13",
"@salesforce/cli-plugins-testkit": "^3.3.1",
"@salesforce/dev-config": "^3.1.0",
Expand All @@ -88,7 +88,7 @@
"eslint-config-prettier": "^8.8.0",
"eslint-config-salesforce-typescript": "^1.1.1",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsdoc": "^40.1.0",
"eslint-plugin-jsdoc": "^40.1.1",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.3",
"jest": "^29.5.0",
Expand Down
11 changes: 11 additions & 0 deletions src/service/inFolderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
META_REGEX,
METAFILE_SUFFIX,
} = require('../utils/metadataConstants')
const { GIT_PATH_SEP } = require('../utils/gitConstants')
const { cleanUpPackageMember } = require('../utils/packageHelper')
const { join, parse, sep } = require('path')
const { readDir } = require('../utils/fsHelper')
Expand Down Expand Up @@ -50,6 +51,16 @@ class InFolderHandler extends StandardHandler {

return cleanUpPackageMember(packageMember)
}

_isProcessable() {
const parsedLine = parse(this.line)
const parentFolder = parsedLine.dir.split(GIT_PATH_SEP).pop()
return (
super._isProcessable() ||
parentFolder !== this.type ||
this.ext.endsWith(INFOLDER_SUFFIX)
)
}
}

module.exports = InFolderHandler
29 changes: 19 additions & 10 deletions src/service/inResourceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
const StandardHandler = require('./standardHandler')
const { join, parse } = require('path')
const { pathExists, readDir } = require('../utils/fsHelper')
const { META_REGEX, METAFILE_SUFFIX } = require('../utils/metadataConstants')
const { META_REGEX } = require('../utils/metadataConstants')
const { GIT_PATH_SEP } = require('../utils/gitConstants')
const { cleanUpPackageMember } = require('../utils/packageHelper')

const STATICRESOURCE_TYPE = 'staticresources'
Expand All @@ -12,20 +13,21 @@ class ResourceHandler extends StandardHandler {
constructor(line, type, work, metadata) {
super(line, type, work, metadata)
}

async handleAddition() {
await super.handleAddition()
if (!this.config.generateDelta) return
const [, srcPath, elementName] = this._parseLine()
const [, srcPath, elementFile] = this._parseLine()
await this._buildElementMap(srcPath)

const matchingFiles = this._buildMatchingFiles(elementName)
const matchingFiles = this._buildMatchingFiles(elementFile)
const elementName = parse(elementFile).name
await Promise.all(
elementSrc
.get(srcPath)
.filter(
src =>
(this.type === STATICRESOURCE_TYPE &&
src.startsWith(parse(elementName).name)) ||
src.startsWith(elementName)) ||
matchingFiles.includes(src)
)
.map(src => this._copyWithMetaFile(join(srcPath, src)))
Expand Down Expand Up @@ -59,11 +61,7 @@ class ResourceHandler extends StandardHandler {
const parsedElementName = parse(elementName).name
const matchingFiles = [parsedElementName]
if (this.metadata.get(this.type).metaFile) {
matchingFiles.push(
`${parsedElementName}.${
this.metadata.get(this.type).suffix
}${METAFILE_SUFFIX}`
)
matchingFiles.push(this._getRelativeMetadataXmlFileName(elementName))
}
return matchingFiles
}
Expand All @@ -77,6 +75,17 @@ class ResourceHandler extends StandardHandler {
)
}
}

_isProcessable() {
const parsedLine = parse(this.line)
const parentFolder = parsedLine.dir.split(GIT_PATH_SEP).pop()

return (
super._isProcessable() ||
parentFolder !== this.type ||
!parsedLine.name.startsWith('.')
)
}
}

module.exports = ResourceHandler
17 changes: 15 additions & 2 deletions src/service/standardHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class StandardHandler {
[MODIFICATION]: this.handleModification,
}

this.ext = parse(this.line).ext.substring(1)
this.ext = parse(this.line)
.base.replace(METAFILE_SUFFIX, '')
.split('.')
.pop()
}

async handle() {
if (this.handlerMap[this.changeType]) {
if (this.handlerMap[this.changeType] && this._isProcessable()) {
try {
await this.handlerMap[this.changeType].apply(this)
} catch (error) {
Expand Down Expand Up @@ -123,6 +126,16 @@ class StandardHandler {
)
)
}

_getRelativeMetadataXmlFileName(path) {
return `${parse(path).base.replace(this.ext, '').replace(/\.$/, '')}.${
this.metadata.get(this.type).suffix
}${METAFILE_SUFFIX}`
}

_isProcessable() {
return this.metadata.get(this.type).suffix === this.ext
}
}

module.exports = StandardHandler
4 changes: 4 additions & 0 deletions src/service/waveHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class WaveHandler extends StandardHandler {
member: this._getElementName(),
})
}

_isProcessable() {
return super._isProcessable() || WAVE_SUBTYPE.has(this.ext)
}
}

module.exports = WaveHandler
6 changes: 3 additions & 3 deletions src/utils/fsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { readFile: fsReadFile } = require('fs').promises
const { isAbsolute, join, relative } = require('path')
const { outputFile } = require('fs-extra')
const { spawn } = require('child_process')
const { UTF8_ENCODING } = require('./gitConstants')
const { GIT_PATH_SEP, UTF8_ENCODING } = require('./gitConstants')
const {
EOLRegex,
getStreamContent,
Expand All @@ -13,7 +13,7 @@ const {
const FOLDER = 'tree'

const showCmd = ['--no-pager', 'show']
const gitPathSeparatorNormalizer = path => path?.replace(/\\+/g, '/')
const gitPathSeparatorNormalizer = path => path?.replace(/\\+/g, GIT_PATH_SEP)
const copiedFiles = new Set()

const copyFiles = async (config, src) => {
Expand Down Expand Up @@ -91,7 +91,7 @@ async function* scan(dir, config) {
const entries = await readDir(dir, config)
for (const file of entries) {
const filePath = join(dir, file)
if (file.endsWith('/')) {
if (file.endsWith(GIT_PATH_SEP)) {
yield* scan(filePath, config)
} else {
yield filePath
Expand Down
1 change: 1 addition & 0 deletions src/utils/gitConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports.TAG_REF_TYPE = TAG_REF_TYPE
module.exports.POINTER_REF_TYPES = [COMMIT_REF_TYPE, TAG_REF_TYPE]
module.exports.GIT_DIFF_TYPE_REGEX = /^.\s+/u
module.exports.GIT_FOLDER = '.git'
module.exports.GIT_PATH_SEP = '/'
module.exports.MINUS = '-'
module.exports.IGNORE_WHITESPACE_PARAMS = [
'--ignore-all-space',
Expand Down
Loading

0 comments on commit f4c5483

Please sign in to comment.