Skip to content

Commit

Permalink
Merge pull request #232 from sasjs/update-configuration-interface
Browse files Browse the repository at this point in the history
feat: add sasjsBuildFolder and sasjsResultsFolder to configuration
  • Loading branch information
allanbowe authored Feb 23, 2023
2 parents 6f818da + c4a9df5 commit cd41851
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export interface Config {

export interface BuildConfig extends Config {
buildOutputFileName: string
buildOutputFolder?: string
buildResultsFolder?: string
}

export interface ServiceConfig extends Config {
Expand Down
2 changes: 2 additions & 0 deletions src/types/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { HttpsAgentOptions } from './httpsAgentOptions'

export interface Configuration {
$schema?: string
sasjsBuildFolder?: string
sasjsResultsFolder?: string
httpsAgentOptions?: HttpsAgentOptions
docConfig?: DocConfig
buildConfig?: BuildConfig
Expand Down
112 changes: 110 additions & 2 deletions src/types/target.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,116 @@ describe('Target', () => {
expect(target.syncFolder).toBeUndefined()
})

it('should create an instance when the JSON is valid and contains sasjsBuildFolder', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
sasjsBuildFolder: 'sasjsbuild'
})

expect(target).toBeTruthy()
expect(target instanceof Target).toEqual(true)
expect(target.name).toEqual('test')
expect(target.serverUrl).toEqual('')
expect(target.serverType).toEqual(ServerType.Sas9)
expect(target.appLoc).toEqual('/test')
expect(target.sasjsBuildFolder).toEqual('sasjsbuild')
})

it('should convert an instance to JSON when containing sasjsBuildFolder', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
sasjsBuildFolder: 'sasjsbuild'
})

const json = target.toJson()
expect(json).toBeTruthy()
expect(json.name).toEqual('test')
expect(json.serverUrl).toEqual('')
expect(json.serverType).toEqual(ServerType.Sas9)
expect(json.appLoc).toEqual('/test')
expect(json.sasjsBuildFolder).toEqual('sasjsbuild')
})

it('should create an instance when the JSON is valid and contains sasjsResultsFolder', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
sasjsResultsFolder: 'sasjsresults'
})

expect(target).toBeTruthy()
expect(target instanceof Target).toEqual(true)
expect(target.name).toEqual('test')
expect(target.serverUrl).toEqual('')
expect(target.serverType).toEqual(ServerType.Sas9)
expect(target.appLoc).toEqual('/test')
expect(target.sasjsResultsFolder).toEqual('sasjsresults')
})

it('should convert an instance to JSON containing sasjsResultsFolder', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
sasjsResultsFolder: 'sasjsresults'
})

const json = target.toJson()
expect(json).toBeTruthy()
expect(json.name).toEqual('test')
expect(json.serverUrl).toEqual('')
expect(json.serverType).toEqual(ServerType.Sas9)
expect(json.appLoc).toEqual('/test')
expect(json.sasjsResultsFolder).toEqual('sasjsresults')
})

it('should create an instance when the JSON is valid and contains syncDirectories', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
syncDirectories: [{ local: 'local', remote: 'remote' }]
})

expect(target).toBeTruthy()
expect(target instanceof Target).toEqual(true)
expect(target.name).toEqual('test')
expect(target.serverUrl).toEqual('')
expect(target.serverType).toEqual(ServerType.Sas9)
expect(target.appLoc).toEqual('/test')
expect(target.syncDirectories).toEqual([
{ local: 'local', remote: 'remote' }
])
})

it('should convert an instance to JSON containing syncDirectories', () => {
const target = new Target({
name: 'test',
serverUrl: '',
serverType: ServerType.Sas9,
appLoc: '/test',
syncDirectories: [{ local: 'local', remote: 'remote' }]
})

const json = target.toJson()
expect(json).toBeTruthy()
expect(json.name).toEqual('test')
expect(json.serverUrl).toEqual('')
expect(json.serverType).toEqual(ServerType.Sas9)
expect(json.appLoc).toEqual('/test')
expect(json.syncDirectories).toEqual([{ local: 'local', remote: 'remote' }])
})

it('should convert an instance to json containing build config', () => {
const target = new Target({
name: 'test',
Expand Down Expand Up @@ -448,8 +558,6 @@ describe('Target', () => {
initProgram: '',
termProgram: '',
buildOutputFileName: `${target.name}.sas`,
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
macroVars: {}
})
expect(json.jobConfig).toEqual({
Expand Down
42 changes: 39 additions & 3 deletions src/types/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
validateAuthConfigSas9,
validateTestConfig,
validateSyncFolder,
validateSyncDirectories
validateSyncDirectories,
validateSasjsBuildFolder,
validateSasjsResultsFolder,
DEFAULT_SASJS_BUILD_FOLDER,
DEFAULT_SASJS_RESULTS_FOLDER
} from './targetValidators'
import { Configuration } from '../types/configuration'

Expand All @@ -59,6 +63,8 @@ export interface TargetJson {
isDefault?: boolean
testConfig?: TestConfig
syncDirectories?: SyncDirectoryMap[]
sasjsBuildFolder?: string
sasjsResultsFolder?: string
}

export class Target implements TargetJson {
Expand Down Expand Up @@ -174,6 +180,16 @@ export class Target implements TargetJson {
}
private _syncDirectories: SyncDirectoryMap[] | undefined

get sasjsBuildFolder(): string | undefined {
return this._sasjsBuildFolder
}
private _sasjsBuildFolder: string | undefined

get sasjsResultsFolder(): string | undefined {
return this._sasjsResultsFolder
}
private _sasjsResultsFolder: string | undefined

constructor(json: any, config: Configuration = {}) {
try {
if (!json) {
Expand Down Expand Up @@ -272,6 +288,16 @@ export class Target implements TargetJson {
if (json.syncDirectories && json.syncDirectories.length) {
this._syncDirectories = validateSyncDirectories(json.syncDirectories)
}

if (json.sasjsBuildFolder) {
this._sasjsBuildFolder = validateSasjsBuildFolder(json.sasjsBuildFolder)
}

if (json.sasjsResultsFolder) {
this._sasjsResultsFolder = validateSasjsResultsFolder(
json.sasjsResultsFolder
)
}
} catch (e) {
throw new Error(`Error parsing target: ${(e as Error).message}`)
}
Expand Down Expand Up @@ -311,6 +337,18 @@ export class Target implements TargetJson {
json.authConfigSas9 = this.authConfigSas9
}

if (this.sasjsBuildFolder) {
json.sasjsBuildFolder = this.sasjsBuildFolder
} else if (withDefaults) {
json.sasjsBuildFolder = DEFAULT_SASJS_BUILD_FOLDER
}

if (this.sasjsResultsFolder) {
json.sasjsResultsFolder = this.sasjsResultsFolder
} else if (withDefaults) {
json.sasjsResultsFolder = DEFAULT_SASJS_RESULTS_FOLDER
}

if (this.syncDirectories) {
json.syncDirectories = this.syncDirectories
} else if (withDefaults) {
Expand All @@ -324,8 +362,6 @@ export class Target implements TargetJson {
initProgram: '',
termProgram: '',
buildOutputFileName: `${this.name}.sas`,
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
macroVars: {}
}

Expand Down
52 changes: 41 additions & 11 deletions src/types/targetValidators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import {
validateTestConfig,
validateAuthConfigSas9,
validateSyncFolder,
validateSyncDirectories
validateSyncDirectories,
validateSasjsBuildFolder,
validateSasjsResultsFolder,
DEFAULT_SASJS_BUILD_FOLDER,
DEFAULT_SASJS_RESULTS_FOLDER
} from './targetValidators'

describe('validateTargetName', () => {
Expand Down Expand Up @@ -233,8 +237,6 @@ describe('validateBuildConfig', () => {

it('should use the default when build output filename is undefined', () => {
expect(validateBuildConfig({} as unknown as BuildConfig, 'test')).toEqual({
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
buildOutputFileName: 'test.sas',
initProgram: '',
termProgram: '',
Expand Down Expand Up @@ -267,17 +269,13 @@ describe('validateBuildConfig', () => {
expect(
validateBuildConfig(
{
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
buildOutputFileName: 'output.sas',
initProgram: 'test',
termProgram: null
} as unknown as BuildConfig,
'test'
)
).toEqual({
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
buildOutputFileName: 'output.sas',
initProgram: 'test',
termProgram: '',
Expand All @@ -289,8 +287,6 @@ describe('validateBuildConfig', () => {
expect(
validateBuildConfig(
{
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
buildOutputFileName: 'output.sas',
initProgram: 'test',
termProgram: 'test',
Expand All @@ -299,8 +295,6 @@ describe('validateBuildConfig', () => {
'test'
)
).toEqual({
buildOutputFolder: 'sasjsbuild',
buildResultsFolder: 'sasjsresults',
buildOutputFileName: 'output.sas',
initProgram: 'test',
termProgram: 'test',
Expand Down Expand Up @@ -1132,3 +1126,39 @@ describe('validateSyncFolder', () => {
expect(validateSyncFolder('some/path')).toEqual('some/path')
})
})

describe('validateSasjsBuildFolder', () => {
it('should return sasjsBuildFolder', () => {
expect(validateSasjsBuildFolder('build')).toEqual('build')
})

it('should return default when provided value is blank string', () => {
expect(validateSasjsBuildFolder('')).toEqual(DEFAULT_SASJS_BUILD_FOLDER)
})

it('should throw an error when non-string value is provided', () => {
expect(() =>
validateSasjsBuildFolder(123 as unknown as string)
).toThrowError(
`Invalid type of value (number) is provided for property 'sasjsBuildFolder' in config. Required is string.`
)
})
})

describe('validateSasjsResultsFolder', () => {
it('should return the provided value', () => {
expect(validateSasjsResultsFolder('results')).toEqual('results')
})

it('should return default when provided value is blank string', () => {
expect(validateSasjsResultsFolder('')).toEqual(DEFAULT_SASJS_RESULTS_FOLDER)
})

it('should throw an error when non-string value is provided', () => {
expect(() =>
validateSasjsResultsFolder(123 as unknown as string)
).toThrowError(
`Invalid type of value (number) is provided for property 'sasjsResultsFolder' in config. Required is string.`
)
})
})
34 changes: 27 additions & 7 deletions src/types/targetValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const DEFAULT_CONTEXT_NAME = 'SAS Job Execution compute context'
const DEFAULT_SERVER_NAME = 'SASApp'
const DEFAULT_REPOSITORY_NAME = 'Foundation'

export const DEFAULT_SASJS_BUILD_FOLDER = 'sasjsbuild'
export const DEFAULT_SASJS_RESULTS_FOLDER = 'sasjsresults'

export const validateServerType = (serverType: any): ServerType => {
if (!serverType) {
throw new Error(
Expand Down Expand Up @@ -194,13 +197,6 @@ export const validateBuildConfig = (
if (!buildConfig) {
throw new Error('Invalid build config: JSON cannot be null or undefined.')
}
if (!buildConfig.buildResultsFolder) {
buildConfig.buildResultsFolder = 'sasjsresults'
}

if (!buildConfig.buildOutputFolder) {
buildConfig.buildOutputFolder = 'sasjsbuild'
}

if (!buildConfig.buildOutputFileName) {
buildConfig.buildOutputFileName = `${defaultName}.sas`
Expand Down Expand Up @@ -402,3 +398,27 @@ export const validateSyncDirectories = (

return syncDirectories
}

export const validateSasjsBuildFolder = (folderName: any) => {
if (typeof folderName !== 'string') {
throw new Error(
`Invalid type of value (${typeof folderName}) is provided for property 'sasjsBuildFolder' in config. Required is string.`
)
}

if (!folderName) return DEFAULT_SASJS_BUILD_FOLDER

return folderName
}

export const validateSasjsResultsFolder = (folderName: any) => {
if (typeof folderName !== 'string') {
throw new Error(
`Invalid type of value (${typeof folderName}) is provided for property 'sasjsResultsFolder' in config. Required is string.`
)
}

if (!folderName) return DEFAULT_SASJS_RESULTS_FOLDER

return folderName
}

0 comments on commit cd41851

Please sign in to comment.