Skip to content

Commit 7d07e96

Browse files
committed
refactor(getos): move function from utils to getos only used there
1 parent a5626a6 commit 7d07e96

File tree

4 files changed

+82
-81
lines changed

4 files changed

+82
-81
lines changed

packages/mongodb-memory-server-core/src/util/__tests__/utils.test.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -83,61 +83,6 @@ describe('utils', () => {
8383
});
8484
});
8585

86-
describe('tryReleaseFile', () => {
87-
it('should return an Parsed Output', async () => {
88-
expect.assertions(2);
89-
const shouldOutput = 'HelloThere';
90-
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(shouldOutput);
91-
92-
await expect(
93-
utils.tryReleaseFile('/some/path', (output) => {
94-
expect(output).toEqual(shouldOutput);
95-
96-
return { hello: output } as any;
97-
})
98-
).resolves.toEqual({ hello: shouldOutput });
99-
});
100-
101-
it('should return "undefined" if "ENOENT"', async () => {
102-
const retError = new Error();
103-
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
104-
retError.code = 'ENOENT';
105-
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
106-
107-
await expect(
108-
utils.tryReleaseFile('/some/path', () => {
109-
fail('This Function should never run');
110-
})
111-
).resolves.toBeUndefined();
112-
});
113-
114-
it('should return "undefined" if "EACCES"', async () => {
115-
const retError = new Error();
116-
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
117-
retError.code = 'EACCES';
118-
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
119-
120-
await expect(
121-
utils.tryReleaseFile('/some/path', () => {
122-
fail('This Function should never run');
123-
})
124-
).resolves.toBeUndefined();
125-
});
126-
127-
it('should throw an error if error is not "ENOENT" or "EACCES"', async () => {
128-
const retError = new Error();
129-
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
130-
retError.code = 'EPERM';
131-
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
132-
133-
await expect(
134-
utils.tryReleaseFile('/some/path', () => {
135-
fail('This Function should never run');
136-
})
137-
).rejects.toThrow(retError);
138-
});
139-
});
140-
14186
describe('getHost', () => {
14287
it('should correctly extract host & port', () => {
14388
expect(utils.getHost('mongodb://user:pass@localhost:port/authdb?queryoptions=1')).toEqual(

packages/mongodb-memory-server-core/src/util/getos/__tests__/getos.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as getos from '../index';
2+
import { promises as fspromises } from 'fs';
23

34
// This File will use example input, it should be formatted with multiple template strings
45

@@ -173,6 +174,61 @@ LOGO="distributor-logo-Tumbleweed"`;
173174
});
174175
});
175176

177+
describe('tryReleaseFile', () => {
178+
it('should return an Parsed Output', async () => {
179+
expect.assertions(2);
180+
const shouldOutput = 'HelloThere';
181+
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(shouldOutput);
182+
183+
await expect(
184+
getos.tryReleaseFile('/some/path', (output) => {
185+
expect(output).toEqual(shouldOutput);
186+
187+
return { hello: output } as any;
188+
})
189+
).resolves.toEqual({ hello: shouldOutput });
190+
});
191+
192+
it('should return "undefined" if "ENOENT"', async () => {
193+
const retError = new Error();
194+
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
195+
retError.code = 'ENOENT';
196+
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
197+
198+
await expect(
199+
getos.tryReleaseFile('/some/path', () => {
200+
fail('This Function should never run');
201+
})
202+
).resolves.toBeUndefined();
203+
});
204+
205+
it('should return "undefined" if "EACCES"', async () => {
206+
const retError = new Error();
207+
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
208+
retError.code = 'EACCES';
209+
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
210+
211+
await expect(
212+
getos.tryReleaseFile('/some/path', () => {
213+
fail('This Function should never run');
214+
})
215+
).resolves.toBeUndefined();
216+
});
217+
218+
it('should throw an error if error is not "ENOENT" or "EACCES"', async () => {
219+
const retError = new Error();
220+
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
221+
retError.code = 'EPERM';
222+
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
223+
224+
await expect(
225+
getos.tryReleaseFile('/some/path', () => {
226+
fail('This Function should never run');
227+
})
228+
).rejects.toThrow(retError);
229+
});
230+
});
231+
176232
describe('extra os tests', () => {
177233
it('should parse cachyos', () => {
178234
// output taken from https://github.com/typegoose/mongodb-memory-server/issues/947

packages/mongodb-memory-server-core/src/util/getos/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { platform } from 'os';
22
import debug from 'debug';
3-
import { isNullOrUndefined, tryReleaseFile } from '../utils';
3+
import { errorWithCode, isNullOrUndefined } from '../utils';
4+
import { promises as fspromises } from 'fs';
45

56
const log = debug('MongoMS:getos');
67

@@ -155,3 +156,27 @@ export function parseOS(input: string): LinuxOS {
155156
id_like: input.match(OSRegex.id_like)?.[1].toLocaleLowerCase().split(' '),
156157
};
157158
}
159+
160+
/**
161+
* Try to read a release file path and apply a parser to the output
162+
* @param path The Path to read for an release file
163+
* @param parser An function to parse the output of the file
164+
*/
165+
export async function tryReleaseFile(
166+
path: string,
167+
parser: (output: string) => LinuxOS | undefined
168+
): Promise<LinuxOS | undefined> {
169+
try {
170+
const output = await fspromises.readFile(path);
171+
172+
return parser(output.toString());
173+
} catch (err) {
174+
if (errorWithCode(err) && !['ENOENT', 'EACCES'].includes(err.code)) {
175+
throw err;
176+
}
177+
178+
log(`tryReleaseFile: "${path}" does not exist`);
179+
180+
return undefined;
181+
}
182+
}

packages/mongodb-memory-server-core/src/util/utils.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import debug from 'debug';
22
import { ChildProcess } from 'child_process';
33
import { AutomaticAuth } from '../MongoMemoryServer';
44
import { promises as fspromises, Stats, constants } from 'fs';
5-
import { LinuxOS } from './getos';
65
import {
76
AssertionFallbackError,
87
BinaryNotFoundError,
@@ -219,30 +218,6 @@ export async function pathExists(path: string): Promise<boolean> {
219218
return !isNullOrUndefined(await statPath(path));
220219
}
221220

222-
/**
223-
* Try to read an release file path and apply an parser to the output
224-
* @param path The Path to read for an release file
225-
* @param parser An function to parse the output of the file
226-
*/
227-
export async function tryReleaseFile(
228-
path: string,
229-
parser: (output: string) => LinuxOS | undefined
230-
): Promise<LinuxOS | undefined> {
231-
try {
232-
const output = await fspromises.readFile(path);
233-
234-
return parser(output.toString());
235-
} catch (err) {
236-
if (errorWithCode(err) && !['ENOENT', 'EACCES'].includes(err.code)) {
237-
throw err;
238-
}
239-
240-
log(`tryReleaseFile: "${path}" does not exist`);
241-
242-
return undefined;
243-
}
244-
}
245-
246221
/**
247222
* Cleanup interface to provide easy to understand arguments for clean-up
248223
*/

0 commit comments

Comments
 (0)