Skip to content

Commit

Permalink
feat(compile): compile single test (#1337)
Browse files Browse the repository at this point in the history
* chore(compile): improved imports of internal funcs

* feat(compile): added single test file compilation
  • Loading branch information
YuryShkoda authored Mar 30, 2023
1 parent aee63ef commit 0d0e7fb
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 98 deletions.
5 changes: 1 addition & 4 deletions src/commands/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import { compressAndSave } from '../../utils/compressAndSave'
import { getMacroFolders, getStreamConfig } from '../../utils/config'
import { isSasFile } from '../../utils/file'
import { compile } from '../compile/compile'
import {
getCompileTree,
loadDependencies
} from '../compile/internal/loadDependencies'
import { getCompileTree, loadDependencies } from '../compile/internal'
import { getBuildInit, getBuildTerm } from './internal/config'
import { getLaunchPageCode } from './internal/getLaunchPageCode'

Expand Down
20 changes: 9 additions & 11 deletions src/commands/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ import {
import { isSasFile } from '../../utils/file'
import { createWebAppServices } from '../web/web'
import * as compileModule from './compile'
import { checkCompileStatus } from './internal/checkCompileStatus'
import { compileFile } from './internal/compileFile'
import { getAllFolders, SasFileType } from './internal/getAllFolders'

import {
checkCompileStatus,
compileFile,
compileTestFile,
compileTestFlow,
copyTestMacroFiles
} from './internal/compileTestFile'
import { copySyncFolder } from './internal/copySyncFolder'
import {
copyTestMacroFiles,
copySyncFolder,
getAllFolders,
SasFileType,
getDestinationJobPath,
getDestinationServicePath
} from './internal/getDestinationPath'
import { getCompileTree } from './internal/loadDependencies'
getDestinationServicePath,
getCompileTree
} from './internal'

export async function compile(target: Target, forceCompile = false) {
const result = await checkCompileStatus(target, ['tests'])
Expand Down
12 changes: 10 additions & 2 deletions src/commands/compile/compileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getAbsolutePath } from '@sasjs/utils'
enum CompileSubCommand {
Job = 'job',
Service = 'service',
Test = 'test',
Identify = 'identify'
}

Expand Down Expand Up @@ -41,15 +42,18 @@ export class CompileCommand extends TargetCommand {
constructor(args: string[]) {
let parseOptions = {}
const subCommand = args[3]

if (
[
CompileSubCommand.Job,
CompileSubCommand.Identify,
CompileSubCommand.Service
CompileSubCommand.Service,
CompileSubCommand.Test,
CompileSubCommand.Identify
].includes(subCommand as CompileSubCommand)
) {
parseOptions = subCommandParseOptions
}

super(args, { parseOptions, syntax, usage, description, examples, aliases })
}

Expand Down Expand Up @@ -96,6 +100,7 @@ export class CompileCommand extends TargetCommand {
process.logger?.success(
`The project was successfully compiled for ${target.serverType} using target '${target.name}'\nThe compile output is located in the ${buildDestinationFolder} directory.`
)

return ReturnCode.Success
})
.catch((err) => {
Expand All @@ -108,6 +113,7 @@ export class CompileCommand extends TargetCommand {
async executeSingleFileCompile() {
const { target } = await this.getTargetInfo()
const output = await this.output

return await compileSingleFile(
target,
this.parsed.subCommand as string,
Expand All @@ -118,10 +124,12 @@ export class CompileCommand extends TargetCommand {
process.logger?.success(
`Source has been successfully compiled!\nThe compiled output is located at: ${res.destinationPath}`
)

return ReturnCode.Success
})
.catch((err) => {
displayError(err, 'Error compiling source.')

return ReturnCode.InternalError
})
}
Expand Down
63 changes: 45 additions & 18 deletions src/commands/compile/compileSingleFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ import {
getAbsolutePath,
SASJsFileType
} from '@sasjs/utils'
import { compileFile } from './internal/compileFile'
import { identifySasFile } from './internal/identifySasFile'
import { getCompileTree } from './internal/loadDependencies'
import { prefixMessage } from '@sasjs/utils/error'
import {
compileFile,
identifySasFile,
getCompileTree,
compileTestFile
} from './internal'

export enum CompileSingleFileSubCommands {
Job = 'job',
Service = 'service',
Test = 'test'
}

const isCompileSingleFileSubCommands = (command: string) =>
(Object.values(CompileSingleFileSubCommands) as string[]).includes(command)

export async function compileSingleFile(
target: Target,
Expand All @@ -21,15 +34,13 @@ export async function compileSingleFile(
insertProgramVar: boolean = false,
currentFolder?: string
) {
const subCommands = {
job: 'job',
service: 'service'
}

if (!subCommands.hasOwnProperty(subCommand) && subCommand !== 'identify') {
if (
!isCompileSingleFileSubCommands(subCommand) &&
subCommand !== 'identify'
) {
throw new Error(
`Unsupported context command. Supported commands are:\n${Object.keys(
subCommands
`Unsupported context command. Supported commands are:\n${Object.values(
CompileSingleFileSubCommands
).join('\n')}`
)
}
Expand All @@ -49,27 +60,34 @@ export async function compileSingleFile(
)

if (!(await validateSourcePath(sourcePath))) {
throw new Error(`Provide a path to source file (eg '${commandExample}')`)
throw new Error(
`Provide a valid path to source file (eg '${commandExample}')`
)
}

if (subCommand === 'identify') {
subCommand = await identifySasFile(target, sourcePath)
subCommand = await identifySasFile(target, sourcePath).catch((err) => {
throw prefixMessage(err, 'Single file compilation failed. ')
})
}

process.logger?.info(`Compiling source file:\n- ${sourcePath}`)

let outputPathParts = output.split(path.sep)
const leafFolderName = source.split(path.sep).pop() as string
const outputPathParts = output.split(path.sep)
outputPathParts.pop(), outputPathParts.pop()

const leafFolderName = source.split(path.sep).pop() as string
const parentOutputFolder = outputPathParts.join(path.sep)

const pathExists = await fileExists(parentOutputFolder)

if (pathExists) await deleteFolder(parentOutputFolder)

await createFolder(output)

const sourceFileName = sourcePath.split(path.sep).pop() as string
const destinationPath = path.join(output, sourceFileName)

await copy(sourcePath, destinationPath)

const sourceFileNameWithoutExt = sourceFileName.split('.')[0]
Expand All @@ -86,7 +104,7 @@ export async function compileSingleFile(
.join(path.sep)

switch (subCommand) {
case subCommands.service:
case CompileSingleFileSubCommands.Service:
await compileFile(
target,
destinationPath,
Expand All @@ -98,7 +116,7 @@ export async function compileSingleFile(
sourceFolder
)
break
case subCommands.job:
case CompileSingleFileSubCommands.Job:
await compileFile(
target,
destinationPath,
Expand All @@ -110,7 +128,16 @@ export async function compileSingleFile(
sourceFolder
)
break
default:
case CompileSingleFileSubCommands.Test:
await compileTestFile(
target,
sourcePath,
programVar,
undefined,
false,
compileTree,
destinationPath
)
break
}

Expand Down
7 changes: 4 additions & 3 deletions src/commands/compile/internal/checkCompileStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Target, asyncForEach, folderExists } from '@sasjs/utils'
import { compareFolders } from './compareFolders'
import { getAllFolders, SasFileType } from './getAllFolders'
import {
compareFolders,
getAllFolders,
SasFileType,
getDestinationJobPath,
getDestinationServicePath
} from './getDestinationPath'
} from './'

export async function checkCompileStatus(
target: Target,
Expand Down
8 changes: 3 additions & 5 deletions src/commands/compile/internal/compileFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createFile, readFile, isTestFile, CompileTree } from '@sasjs/utils'
import { Target, ServerType, SASJsFileType } from '@sasjs/utils/types'
import { ServerTypeError } from '@sasjs/utils/error'
import { loadDependencies } from './loadDependencies'
import { getServerType } from './getServerType'
import { createFile, isTestFile, CompileTree } from '@sasjs/utils'
import { Target, SASJsFileType } from '@sasjs/utils/types'
import { loadDependencies } from './'
import path from 'path'

/**
Expand Down
41 changes: 22 additions & 19 deletions src/commands/compile/internal/compileTestFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import path from 'path'
import { Coverage, CoverageState, CoverageType, TestFlow } from '../../../types'
import { getMacroFolders, getProgramFolders } from '../../../utils/config'
import { sasFileRegExp } from '../../../utils/file'
import { loadDependencies } from './loadDependencies'
import { loadDependencies } from './'

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

Expand All @@ -30,7 +30,8 @@ export async function compileTestFile(
testVar: string = '',
saveToRoot: boolean = true,
removeOriginalFile = true,
compileTree: CompileTree
compileTree: CompileTree,
destinationPath?: string
) {
let dependencies = await loadDependencies(
target,
Expand All @@ -50,23 +51,25 @@ export async function compileTestFile(
.split(path.sep)
.pop()

const destinationPath = path.join(
buildDestinationTestFolder,
saveToRoot
? filePath.split(path.sep).pop() || ''
: filePath
.split(path.sep)
.reduce(
(acc: any, item: any, i: any, arr: any) =>
acc.length
? [...acc, item]
: arr[i - 1] === buildDestinationFolderName
? [...acc, item]
: acc,
[]
)
.join(path.sep)
)
destinationPath =
destinationPath ||
path.join(
buildDestinationTestFolder,
saveToRoot
? filePath.split(path.sep).pop() || ''
: filePath
.split(path.sep)
.reduce(
(acc: any, item: any, i: any, arr: any) =>
acc.length
? [...acc, item]
: arr[i - 1] === buildDestinationFolderName
? [...acc, item]
: acc,
[]
)
.join(path.sep)
)

await createFile(destinationPath, dependencies)

Expand Down
5 changes: 3 additions & 2 deletions src/commands/compile/internal/getAllFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {

export enum SasFileType {
Service = 'service',
Job = 'job'
Job = 'job',
Test = 'test'
}

export const getAllFolders = async (
target: Target,
type: SasFileType,
type: SasFileType.Service | SasFileType.Job,
rootConfig?: Configuration
): Promise<string[]> => {
const configuration = rootConfig || process.sasjsConfig
Expand Down
18 changes: 0 additions & 18 deletions src/commands/compile/internal/getServerType.ts

This file was deleted.

22 changes: 14 additions & 8 deletions src/commands/compile/internal/identifySasFile.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { Target, isTestFile } from '@sasjs/utils'
import { getAllFolders, SasFileType } from './'
import path from 'path'
import { Target } from '@sasjs/utils'
import { getAllFolders, SasFileType } from './getAllFolders'

export const identifySasFile = async (
target: Target,
sourcePath: string
): Promise<'job' | 'service'> => {
): Promise<SasFileType> => {
if (isTestFile(sourcePath.split(path.sep).pop() as string)) {
return SasFileType.Test
}

const serviceFolders = await getAllFolders(target, SasFileType.Service)

const isService = serviceFolders.find((folder) => sourcePath.includes(folder))
if (isService) return 'service'
if (serviceFolders.find((folder) => sourcePath.includes(folder))) {
return SasFileType.Service
}

const jobFolders = await getAllFolders(target, SasFileType.Job)

const isJob = jobFolders.find((folder) => sourcePath.includes(folder))
if (isJob) return 'job'
if (jobFolders.find((folder) => sourcePath.includes(folder))) {
return SasFileType.Job
}

throw 'Unable to identify file as Service or Job'
throw `Unable to identify file as ${SasFileType.Service}, ${SasFileType.Job} or ${SasFileType.Test}`
}
9 changes: 9 additions & 0 deletions src/commands/compile/internal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './checkCompileStatus'
export * from './compareFolders'
export * from './compileFile'
export * from './compileTestFile'
export * from './copySyncFolder'
export * from './getAllFolders'
export * from './getDestinationPath'
export * from './identifySasFile'
export * from './loadDependencies'
2 changes: 1 addition & 1 deletion src/commands/compile/internal/spec/compileTestFile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compileTestFlow } from '../compileTestFile'
import { compileTestFlow } from '../'
import {
Logger,
LogLevel,
Expand Down
Loading

0 comments on commit 0d0e7fb

Please sign in to comment.