-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a694a91
commit dcb4a28
Showing
10 changed files
with
618 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Tools.GitHubActions/VariableSubstitution/src/appSettingsJsonFileReader.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {AppSettingsJsonFileReader} from "./appSettingsJsonFileReader"; | ||
|
||
describe('AppSettingsJsonFileReader', () => { | ||
|
||
it('should throw, when file does not exist', async () => { | ||
|
||
const reader = new AppSettingsJsonFileReader(); | ||
|
||
try { | ||
await reader.readAppSettingsFile('nonexistent.json'); | ||
} catch (error) { | ||
expect(error.message).toMatch("File 'nonexistent.json' cannot be read from disk, possibly it does not exist, or is not accessible?"); | ||
} | ||
}); | ||
|
||
it('should throw, when file is not JSON', async () => { | ||
|
||
const reader = new AppSettingsJsonFileReader(); | ||
const path = `${__dirname}/testing/__data/invalidjson.txt`; | ||
|
||
try { | ||
await reader.readAppSettingsFile(path); | ||
} catch (error) { | ||
expect(error.message).toMatch(`File '${path}' does not contain valid JSON: SyntaxError: Unexpected token 'i', \"invalid\" is not valid JSON`); | ||
} | ||
}); | ||
|
||
it('should return file, when file has no variables', async () => { | ||
|
||
const reader = new AppSettingsJsonFileReader(); | ||
const path = `${__dirname}/testing/__data/emptyjson.json`; | ||
|
||
const file = await reader.readAppSettingsFile(path); | ||
|
||
expect(file).toEqual({}); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
src/Tools.GitHubActions/VariableSubstitution/src/appSettingsJsonFileReader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from "node:fs"; | ||
|
||
export interface IAppSettingsJsonFileReader { | ||
readAppSettingsFile(path: string): Promise<any>; | ||
} | ||
|
||
export class AppSettingsJsonFileReader implements IAppSettingsJsonFileReader { | ||
async readAppSettingsFile(path: string): Promise<any> { | ||
let data: any; | ||
try { | ||
const result = await fs.promises.readFile(path); | ||
data = Buffer.from(result); | ||
} catch (error) { | ||
throw new Error(`File '${path}' cannot be read from disk, possibly it does not exist, or is not accessible?`); | ||
} | ||
|
||
const raw = data.toString(); | ||
try { | ||
return JSON.parse(raw); | ||
} catch (error) { | ||
throw new Error(`File '${path}' does not contain valid JSON: ${error}`); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.