Skip to content

Commit

Permalink
Merge branch 'main' into issue-555
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Saad authored Oct 29, 2021
2 parents a8bcb5e + 178dc9c commit 3267b6e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/error/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { prefixMessage } from './errorModifier'
export * from './errorModifier'
export * from './serverTypeError'
67 changes: 67 additions & 0 deletions src/error/serverTypeError.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ServerTypeError } from './serverTypeError'
import { ServerType } from '../types'

describe('ServerTypeError', () => {
it('should include valid server type', () => {
const validServerType = ServerType.Sas9
const error = new ServerTypeError([validServerType])

expect(error).toBeInstanceOf(ServerTypeError)
expect(error.name).toEqual('ServerTypeError')
expect(error.message).toEqual(
`Invalid server type: valid option is ${validServerType}`
)
})

it('should include 2 valid server types', () => {
const validServerType1 = ServerType.Sas9
const validServerType2 = ServerType.SasViya
const error = new ServerTypeError([validServerType1, validServerType2])

expect(error.message).toEqual(
`Invalid server type: valid options are ${validServerType1} and ${validServerType2}`
)
})

it('should include 3 valid server types', () => {
const validServerType1 = ServerType.Sas9
const validServerType2 = ServerType.SasViya
const validServerType3 = ServerType.Sasjs
const error = new ServerTypeError([
validServerType1,
validServerType2,
validServerType3
])

expect(error.message).toEqual(
`Invalid server type: valid options are ${validServerType1}, ${validServerType2} and ${validServerType3}`
)
})

it('should include unique server types only', () => {
const validServerType1 = ServerType.Sas9
const validServerType2 = ServerType.SasViya
const validServerType3 = ServerType.Sasjs
const validServerType4 = ServerType.Sasjs
const error = new ServerTypeError([
validServerType1,
validServerType2,
validServerType3,
validServerType4
])

expect(error.message).toEqual(
`Invalid server type: valid options are ${validServerType1}, ${validServerType2} and ${validServerType3}`
)
})

it('should include all supported server types if server type was not provided', () => {
const error = new ServerTypeError()

expect(error.message).toEqual(
`Invalid server type: valid options are ${ServerType.SasViya}, ${ServerType.Sas9} and ${ServerType.Sasjs}`
)
})

// TODO: test if super was called
})
25 changes: 25 additions & 0 deletions src/error/serverTypeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ServerType } from '../types'

export class ServerTypeError extends Error {
constructor(validOptions: ServerType[] = []) {
validOptions = [...new Set(validOptions)]

let options = validOptions.length
? validOptions.join(', ').trim()
: [ServerType.SasViya, ServerType.Sas9, ServerType.Sasjs]
.join(', ')
.trim()

options = options.replace(/,\s([^,]*)$/, ' and $1')

super(
`Invalid server type: valid option${
validOptions.length !== 1 ? 's' : ''
} ${validOptions.length !== 1 ? 'are' : 'is'} ${options}`
)

this.name = 'ServerTypeError'

Object.setPrototypeOf(this, ServerTypeError.prototype)
}
}
3 changes: 2 additions & 1 deletion src/types/serverType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ServerType {
SasViya = 'SASVIYA',
Sas9 = 'SAS9'
Sas9 = 'SAS9',
Sasjs = 'SASJS'
}
2 changes: 1 addition & 1 deletion src/types/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class Target implements TargetJson {

if (this.serverType === ServerType.SasViya) {
json.contextName = this.contextName
} else {
} else if (this.serverType === ServerType.Sas9) {
json.serverName = this.serverName
json.repositoryName = this.repositoryName
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/targetValidators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('validateServerType', () => {
expect(() =>
validateServerType('garbage' as unknown as ServerType)
).toThrowError(
`Invalid server type: Supported values for \`serverType\` are ${ServerType.SasViya} and ${ServerType.Sas9}.`
`Invalid server type: Supported values for \`serverType\` are ${ServerType.SasViya}, ${ServerType.Sas9} and ${ServerType.Sasjs}.`
)
})

Expand Down
10 changes: 8 additions & 2 deletions src/types/targetValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ export const validateServerType = (serverType: any): ServerType => {
)
}

if (!(serverType === ServerType.Sas9 || serverType === ServerType.SasViya)) {
if (
!(
serverType === ServerType.Sas9 ||
serverType === ServerType.SasViya ||
serverType === ServerType.Sasjs
)
) {
throw new Error(
`Invalid server type: Supported values for \`serverType\` are ${ServerType.SasViya} and ${ServerType.Sas9}.`
`Invalid server type: Supported values for \`serverType\` are ${ServerType.SasViya}, ${ServerType.Sas9} and ${ServerType.Sasjs}.`
)
}

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"declaration": true,
"outDir": "./build",
"strict": true,
"sourceMap": true
"sourceMap": true,
"downlevelIteration": true
},
"include": [
"src"
Expand Down

0 comments on commit 3267b6e

Please sign in to comment.