Skip to content

Commit

Permalink
Merge pull request #1355 from sasjs/run-issue
Browse files Browse the repository at this point in the history
Fixed run command for SASJS server type
  • Loading branch information
allanbowe authored Jul 17, 2023
2 parents 3b70e09 + a8a01a7 commit 9e12a45
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 16 additions & 11 deletions src/commands/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
86 changes: 86 additions & 0 deletions src/commands/run/spec/run.spec.ts
Original file line number Diff line number Diff line change
@@ -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 (<jest.Mock<SASjs>>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)
}
3 changes: 3 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,13 @@ export const getNodeModulePath = async (module: string): Promise<string> => {
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)
}
}

0 comments on commit 9e12a45

Please sign in to comment.