Skip to content

Commit

Permalink
fix: use process variable instead of hard coded sasjsbuild (#1328)
Browse files Browse the repository at this point in the history
* chore: remove unused moveTestFile function

* chore: remove unused function saveToDefaultLocation

* fix: remove hard coded use of sasjsbuild

* fix: convert file.js to file.ts and remove unused functions
  • Loading branch information
sabhas authored Mar 3, 2023
1 parent 3320046 commit aab2c4b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 274 deletions.
51 changes: 16 additions & 35 deletions src/commands/compile/internal/compileTestFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import {
getLocalOrGlobalConfig
} from '../../../utils/config'

const testsBuildFolder = () => {
return path.join(process.projectDir, 'sasjsbuild', 'tests')
}

const getFileName = (filePath: string) => path.parse(filePath).base

export async function compileTestFile(
Expand All @@ -55,8 +51,15 @@ export async function compileTestFile(

dependencies = `${testVar ? testVar + '\n' : ''}\n${dependencies}`

const { buildDestinationFolder, buildDestinationTestFolder } =
process.sasjsConstants

const buildDestinationFolderName = buildDestinationFolder
.split(path.sep)
.pop()

const destinationPath = path.join(
testsBuildFolder(),
buildDestinationTestFolder,
saveToRoot
? filePath.split(path.sep).pop() || ''
: filePath
Expand All @@ -65,7 +68,7 @@ export async function compileTestFile(
(acc: any, item: any, i: any, arr: any) =>
acc.length
? [...acc, item]
: arr[i - 1] === 'sasjsbuild'
: arr[i - 1] === buildDestinationFolderName
? [...acc, item]
: acc,
[]
Expand All @@ -78,40 +81,18 @@ export async function compileTestFile(
if (removeOriginalFile) await deleteFile(filePath)
}

export async function moveTestFile(filePath: string) {
const fileName = filePath.split(path.sep).pop() as string

if (!isTestFile(fileName)) return

const destinationPath = filePath.replace('sasjsbuild', 'sasjsbuild/tests')

const testDestinationFolder = destinationPath
.split(path.sep)
.slice(0, -1)
.join(path.sep)

if (!(await folderExists(testDestinationFolder))) {
await createFolder(testDestinationFolder)
}

await moveFile(filePath, destinationPath)

const sourceFolder = filePath
.split(path.sep)
.slice(0, filePath.split(path.sep).length - 1)
.join(path.sep)

if ((await listFilesInFolder(sourceFolder)).length === 0) {
await deleteFolder(sourceFolder)
}
}

export async function copyTestMacroFiles(folderAbsolutePath: string) {
const macroFiles = await listFilesAndSubFoldersInFolder(folderAbsolutePath)
const macroTestFiles = macroFiles.filter((item) => testFileRegExp.test(item))

const { buildDestinationTestFolder } = process.sasjsConstants

await asyncForEach(macroTestFiles, async (file) => {
const destinationFile = path.join(testsBuildFolder(), 'macros', file)
const destinationFile = path.join(
buildDestinationTestFolder,
'macros',
file
)

if (!(await fileExists(destinationFile))) {
await copy(path.join(folderAbsolutePath, file), destinationFile)
Expand Down
40 changes: 1 addition & 39 deletions src/commands/compile/internal/spec/compileTestFile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { moveTestFile, compileTestFlow } from '../compileTestFile'
import { compileTestFlow } from '../compileTestFile'
import {
Logger,
LogLevel,
Target,
copy,
readFile,
fileExists,
createFile,
generateTimestamp,
deleteFolder,
ServerType,
Expand Down Expand Up @@ -122,43 +121,6 @@ describe('compileTestFile', () => {
})
})

describe('moveTestFile', () => {
it('should move service test', async () => {
const relativePath = path.join(
'services',
'services',
'admin',
testFileName
)
const buildPath = path.join(__dirname, appName, 'sasjsbuild')
const originalFilePath = path.join(buildPath, relativePath)
const destinationFilePath = path.join(buildPath, 'tests', relativePath)

await createFile(originalFilePath, testBody)

await moveTestFile(originalFilePath)

await expect(fileExists(originalFilePath)).resolves.toEqual(false)
await expect(fileExists(destinationFilePath)).resolves.toEqual(true)
await expect(readFile(destinationFilePath)).resolves.toEqual(testBody)
})

it('should move job test', async () => {
const relativePath = path.join('jobs', 'jobs', testFileName)
const buildPath = path.join(__dirname, appName, 'sasjsbuild')
const originalFilePath = path.join(buildPath, relativePath)
const destinationFilePath = path.join(buildPath, 'tests', relativePath)

await createFile(originalFilePath, testBody)

await moveTestFile(originalFilePath)

await expect(fileExists(originalFilePath)).resolves.toEqual(false)
await expect(fileExists(destinationFilePath)).resolves.toEqual(true)
await expect(readFile(destinationFilePath)).resolves.toEqual(testBody)
})
})

describe('compileTestFlow', () => {
it('should compile test flow', async () => {
const testSetUp = path.join('tests', 'testsetup.sas')
Expand Down
5 changes: 3 additions & 2 deletions src/commands/deploy/internal/executeNonSasScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { isShellScript, isPowerShellScript } from '../../../utils/file'
export async function executeNonSasScript(scriptPath: string) {
const fileExtension = path.extname(scriptPath)

const { buildDestinationResultsLogsFolder } = process.sasjsConstants

const logPath = path.join(
process.projectDir,
'sasjsbuild',
buildDestinationResultsLogsFolder,
path.basename(scriptPath).replace(fileExtension, '.log')
)

Expand Down
6 changes: 3 additions & 3 deletions src/commands/deploy/spec/deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('deploy', () => {

expect(process.logger.success).toHaveBeenCalledWith(
`Shell script execution completed! Log is here: ${path.join(
process.sasjsConstants.buildDestinationFolder,
process.sasjsConstants.buildDestinationResultsLogsFolder,
'script.log'
)}`
)
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('deploy', () => {

expect(process.logger.success).toHaveBeenCalledWith(
`Shell script execution completed! Log is here: ${path.join(
process.sasjsConstants.buildDestinationFolder,
process.sasjsConstants.buildDestinationResultsLogsFolder,
'script.log'
)}`
)
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('deploy', () => {

expect(process.logger.success).toHaveBeenCalledWith(
`PowerShell script execution completed! Log is here: ${path.join(
process.sasjsConstants.buildDestinationFolder,
process.sasjsConstants.buildDestinationResultsLogsFolder,
'script.log'
)}`
)
Expand Down
12 changes: 12 additions & 0 deletions src/commands/fs/spec/fsCommand.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('FSCommand', () => {
.spyOn(FileModule, 'createFile')
.mockImplementation(() => Promise.resolve())

jest
.spyOn(configUtils, 'findTargetInConfiguration')
.mockImplementation(() =>
Promise.resolve({ target: target, isLocal: true })
)

const returnCode = await executeCommandWrapper([
'compile',
'localFolder',
Expand All @@ -50,6 +56,12 @@ describe('FSCommand', () => {
.spyOn(FileModule, 'createFile')
.mockImplementation(() => Promise.reject())

jest
.spyOn(configUtils, 'findTargetInConfiguration')
.mockImplementation(() =>
Promise.resolve({ target: target, isLocal: true })
)

const returnCode = await executeCommandWrapper(['compile', 'localFolder'])
expect(returnCode).toEqual(ReturnCode.InternalError)
expect(process.logger.error).toHaveBeenCalledWith(
Expand Down
155 changes: 0 additions & 155 deletions src/utils/file.js

This file was deleted.

Loading

0 comments on commit aab2c4b

Please sign in to comment.