diff --git a/package.json b/package.json index 16a0a12c..644c8720 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "preinstall": "npm run nodeVersionMessage", "prepare": "git rev-parse --git-dir && git config core.hooksPath ./.git-hooks || true" }, - "testServerTypes": "sasjs,sas9", + "testServerTypes": "sasjs", "release": { "branches": [ "main" diff --git a/src/commands/run/run.ts b/src/commands/run/run.ts index 5a19752e..215457c6 100644 --- a/src/commands/run/run.ts +++ b/src/commands/run/run.ts @@ -82,13 +82,14 @@ export async function runSasCode( await deleteFile(filePath) } - if (target.serverType === ServerType.SasViya) - return await executeOnSasViya(filePath, target, linesToExecute, logFile) - - if (target.serverType === ServerType.Sas9) - return await executeOnSas9(target, linesToExecute, logFile) - - return await executeOnSasJS(filePath, target, linesToExecute, logFile) + switch (target.serverType) { + case ServerType.SasViya: + return await executeOnSasViya(filePath, target, linesToExecute, logFile) + case ServerType.Sas9: + return await executeOnSas9(target, linesToExecute, logFile) + case ServerType.Sasjs: + return await executeOnSasJS(filePath, target, linesToExecute, logFile) + } } async function executeOnSasViya( @@ -176,24 +177,28 @@ async function executeOnSasJS( linesToExecute: string[], logFile: string | undefined ) { + const sasjs = getSASjs(target) const authConfig = (await isSasJsServerInServerMode(target)) ? await getAuthConfig(target) : undefined - const sasjs = getSASjs(target) const fileExtension = path.extname(filePath).slice(1) - const executionResult = await sasjs.executeScript({ + const { log } = await sasjs.executeScript({ linesOfCode: linesToExecute, runTime: fileExtension, authConfig }) + if (!log) { + throw new ErrorResponse('We were not able to fetch the log this time.') + } + process.logger?.success('Job execution completed!') - await createOutputFile(executionResult || '', logFile) + await createOutputFile(log, logFile) - return { log: executionResult } + return { log: log } } async function createOutputFile( diff --git a/src/commands/run/spec/run.spec.ts b/src/commands/run/spec/run.spec.ts new file mode 100644 index 00000000..c2108e1f --- /dev/null +++ b/src/commands/run/spec/run.spec.ts @@ -0,0 +1,86 @@ +import { Target, ServerType } from '@sasjs/utils' +import { runSasCode } from '../run' +import * as utilsModule from '../../../utils/utils' +import * as configModule from '../../../utils/config' +import * as saveLogModule from '../../../utils/saveLog' +import * as fileModule from '@sasjs/utils/file' +import * as getAbsolutePathModule from '@sasjs/utils/file/getAbsolutePath' +import SASjs, { ErrorResponse } from '@sasjs/adapter/node' +import { setConstants, saveLog } from '../../../utils' + +const sasjs = new (>SASjs)() + +describe('sasjs run', () => { + describe('SASJS', () => { + const target = new Target({ + name: 'test', + appLoc: '/Public/test/', + serverType: ServerType.Sasjs, + contextName: 'test context' + }) + const logData = 'SAS log' + const filePath = 'test.sas' + + beforeAll(async () => { + await setConstants(false) + }) + + beforeEach(() => { + setupMocks(filePath, logData) + }) + + it('should saveLog function with correct log data', async () => { + jest + .spyOn(sasjs, 'executeScript') + .mockImplementation(() => Promise.resolve({ log: logData })) + jest.spyOn(saveLogModule, 'saveLog').mockImplementation() + + process.sasjsConstants.buildDestinationResultsFolder = __dirname + + await runSasCode(target, filePath, false, '') + + expect(saveLog).toHaveBeenCalledWith( + logData, + expect.stringMatching(/sasjs-run-\d{14}\.log$/), + '', + false, + undefined + ) + }) + + it('should throw an error if log is not returned by @sasjs/adapter', async () => { + jest + .spyOn(sasjs, 'executeScript') + .mockImplementation(() => Promise.resolve({})) + + const expectedError = new ErrorResponse( + 'We were not able to fetch the log this time.' + ) + + await expect(runSasCode(target, filePath, false, '')).rejects.toEqual( + expectedError + ) + }) + }) +}) + +const setupMocks = (filePath: string, logData: string) => { + jest + .spyOn(getAbsolutePathModule, 'getAbsolutePath') + .mockImplementation(() => filePath) + jest + .spyOn(fileModule, 'readFile') + .mockImplementation(() => Promise.resolve('test SAS code;')) + jest + .spyOn(utilsModule, 'isSasJsServerInServerMode') + .mockImplementation(() => Promise.resolve(true)) + jest.spyOn(configModule, 'getAuthConfig').mockImplementation(() => + Promise.resolve({ + access_token: 'test_access_token', + refresh_token: 'test_refresh_token', + client: 'test_client', + secret: '' + }) + ) + jest.spyOn(configModule, 'getSASjs').mockImplementation((target) => sasjs) +} diff --git a/src/utils/utils.ts b/src/utils/utils.ts index b94e88c2..bab517fb 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -590,10 +590,13 @@ export const getNodeModulePath = async (module: string): Promise => { export const isSasJsServerInServerMode = async (target: Target) => { try { const res = await axios.get(`${target.serverUrl}/SASjsApi/info`) + return res.data.mode === 'server' } catch (error) { const message = `An error occurred while fetching server info from ${target.serverUrl}/SASjsApi/info` + displayError(error, message) + throw new Error(message) } }