Skip to content

Commit

Permalink
Merge pull request #222 from sasjs/extractHashArray
Browse files Browse the repository at this point in the history
fix: move extractHashArray function to utils
  • Loading branch information
allanbowe authored Dec 23, 2022
2 parents 1334b2d + 18e8a7b commit 0b0053e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/fs/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ export function compareHashes(
return hashDifference(localHash, remoteHashMap)
}

export const extractHashArray = (log: string) => {
if (log.includes('>>weboutBEGIN<<')) {
try {
const webout = log
.split(/>>weboutBEGIN<<(\n|\r\n)/)[1]
.split(/>>weboutEND<<(\n|\r\n)/)[0]

const weboutWithoutLF = webout.replace(/\n|\r\n/g, '')
const jsonWebout = JSON.parse(weboutWithoutLF)
return jsonWebout.hashes
} catch (err: any) {
throw new Error(
`An error occurred while extracting hashes array from webout: ${err.message}`
)
}
}
}

const hashDifference = (
localHash: HashedFolder,
remoteHashMap: { [key: string]: string },
Expand Down
2 changes: 1 addition & 1 deletion src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { getHash, compareHashes } from './hash'
export { getHash, compareHashes, extractHashArray } from './hash'
export { generateCompileProgram } from './generateCompileProgram'
export {
generateProgramToGetRemoteHash,
Expand Down
40 changes: 39 additions & 1 deletion src/fs/spec/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import path from 'path'
import { getHash, compareHashes } from '../hash'
import { getHash, compareHashes, extractHashArray } from '../hash'

const webout = {
hashes: [
{
DIRECTORY: '/export/pvs/sasdata/homes/viyademo08f/stephan/extract',
FILE_HASH: 'F041B4AF15A7F844D20F45BA4F5EE1B4',
HASH_DURATION: 0.0069699287,
FILE_PATH:
'/export/pvs/sasdata/homes/viyademo08f/stephan/extract/makedata2.sas',
FILE_OR_FOLDER: 'file',
LEVEL: 1
},
{
DIRECTORY: '/export/pvs/sasdata/homes/viyademo08f/stephan/load',
FILE_HASH: '616C9FA7A70C53AF4D335BE546B62441',
HASH_DURATION: 0.007089138,
FILE_PATH:
'/export/pvs/sasdata/homes/viyademo08f/stephan/load/runjob1.test.sas',
FILE_OR_FOLDER: 'file',
LEVEL: 1
}
]
}

describe('getHash', () => {
it('should return the hash of provided directory', async () => {
Expand All @@ -17,3 +40,18 @@ describe('compareHashes', () => {
expect(hashedDiff).toEqual(hashedFolder)
})
})

describe('extractHashArray', () => {
it('should extract and return a hash array', () => {
const logContent = `>>weboutBEGIN<<\n${JSON.stringify(
webout
)}\n>>weboutEND<<\n`
const hashes = extractHashArray(logContent)
expect(hashes).toEqual(webout.hashes)
})

it('should throw an error when webout is not valid json', () => {
const logContent = `>>weboutBEGIN<<\n${webout}\n>>weboutEND<<\n`
expect(() => extractHashArray(logContent)).toThrowError()
})
})

0 comments on commit 0b0053e

Please sign in to comment.