|
@@ -1,5 +1,5 @@
|
|
|
import childProcess from 'child_process';
|
|
|
-import { getAbsolutePath, readJsonFile, readFile, readdirSync, fileExists, writeFile, createFolder, deleteFolder, runScript, getSeed, ensureAppFolder } from '../fs.helpers';
|
|
|
+import { readJsonFile, readFile, readdirSync, fileExists, writeFile, createFolder, deleteFolder, runScript, getSeed, ensureAppFolder } from '../fs.helpers';
|
|
|
import fs from 'fs-extra';
|
|
|
import { getConfig } from '../../../core/config/TipiConfig';
|
|
|
import { faker } from '@faker-js/faker';
|
|
@@ -11,24 +11,18 @@ beforeEach(() => {
|
|
|
fs.__resetAllMocks();
|
|
|
});
|
|
|
|
|
|
-describe('Test: getAbsolutePath', () => {
|
|
|
- it('should return the absolute path', () => {
|
|
|
- expect(getAbsolutePath('/test')).toBe(`${getConfig().rootFolder}/test`);
|
|
|
- });
|
|
|
-});
|
|
|
-
|
|
|
describe('Test: readJsonFile', () => {
|
|
|
it('should return the json file', () => {
|
|
|
// Arrange
|
|
|
const rawFile = '{"test": "test"}';
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/test-file.json`]: rawFile,
|
|
|
+ ['/runtipi/test-file.json']: rawFile,
|
|
|
};
|
|
|
// @ts-ignore
|
|
|
fs.__createMockFiles(mockFiles);
|
|
|
|
|
|
// Act
|
|
|
- const file = readJsonFile('/test-file.json');
|
|
|
+ const file = readJsonFile('/runtipi/test-file.json');
|
|
|
|
|
|
// Assert
|
|
|
expect(file).toEqual({ test: 'test' });
|
|
@@ -59,13 +53,13 @@ describe('Test: readFile', () => {
|
|
|
it('should return the file', () => {
|
|
|
const rawFile = 'test';
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/test-file.txt`]: rawFile,
|
|
|
+ ['/runtipi/test-file.txt']: rawFile,
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
|
fs.__createMockFiles(mockFiles);
|
|
|
|
|
|
- expect(readFile('/test-file.txt')).toEqual('test');
|
|
|
+ expect(readFile('/runtipi/test-file.txt')).toEqual('test');
|
|
|
});
|
|
|
|
|
|
it('should return empty string if the file does not exist', () => {
|
|
@@ -76,13 +70,13 @@ describe('Test: readFile', () => {
|
|
|
describe('Test: readdirSync', () => {
|
|
|
it('should return the files', () => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/test/test-file.txt`]: 'test',
|
|
|
+ ['/runtipi/test/test-file.txt']: 'test',
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
|
fs.__createMockFiles(mockFiles);
|
|
|
|
|
|
- expect(readdirSync('/test')).toEqual(['test-file.txt']);
|
|
|
+ expect(readdirSync('/runtipi/test')).toEqual(['test-file.txt']);
|
|
|
});
|
|
|
|
|
|
it('should return empty array if the directory does not exist', () => {
|
|
@@ -93,13 +87,13 @@ describe('Test: readdirSync', () => {
|
|
|
describe('Test: fileExists', () => {
|
|
|
it('should return true if the file exists', () => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/test-file.txt`]: 'test',
|
|
|
+ ['/runtipi/test-file.txt']: 'test',
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
|
fs.__createMockFiles(mockFiles);
|
|
|
|
|
|
- expect(fileExists('/test-file.txt')).toBeTruthy();
|
|
|
+ expect(fileExists('/runtipi/test-file.txt')).toBeTruthy();
|
|
|
});
|
|
|
|
|
|
it('should return false if the file does not exist', () => {
|
|
@@ -111,9 +105,9 @@ describe('Test: writeFile', () => {
|
|
|
it('should write the file', () => {
|
|
|
const spy = jest.spyOn(fs, 'writeFileSync');
|
|
|
|
|
|
- writeFile('/test-file.txt', 'test');
|
|
|
+ writeFile('/runtipi/test-file.txt', 'test');
|
|
|
|
|
|
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/test-file.txt`, 'test');
|
|
|
+ expect(spy).toHaveBeenCalledWith('/runtipi/test-file.txt', 'test');
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -123,7 +117,7 @@ describe('Test: createFolder', () => {
|
|
|
|
|
|
createFolder('/test');
|
|
|
|
|
|
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/test`);
|
|
|
+ expect(spy).toHaveBeenCalledWith('/test', { recursive: true });
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -133,7 +127,7 @@ describe('Test: deleteFolder', () => {
|
|
|
|
|
|
deleteFolder('/test');
|
|
|
|
|
|
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/test`, { recursive: true });
|
|
|
+ expect(spy).toHaveBeenCalledWith('/test', { recursive: true });
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -144,14 +138,14 @@ describe('Test: runScript', () => {
|
|
|
|
|
|
runScript('/test', [], callback);
|
|
|
|
|
|
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/test`, [], {}, callback);
|
|
|
+ expect(spy).toHaveBeenCalledWith('/test', [], {}, callback);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('Test: getSeed', () => {
|
|
|
it('should return the seed', () => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/state/seed`]: 'test',
|
|
|
+ ['/runtipi/state/seed']: 'test',
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
@@ -164,7 +158,7 @@ describe('Test: getSeed', () => {
|
|
|
describe('Test: ensureAppFolder', () => {
|
|
|
beforeEach(() => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
+ [`/runtipi/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
};
|
|
|
// @ts-ignore
|
|
|
fs.__createMockFiles(mockFiles);
|
|
@@ -175,15 +169,15 @@ describe('Test: ensureAppFolder', () => {
|
|
|
ensureAppFolder('test');
|
|
|
|
|
|
// Assert
|
|
|
- const files = fs.readdirSync(`${getConfig().rootFolder}/apps/test`);
|
|
|
+ const files = fs.readdirSync('/app/storage/apps/test');
|
|
|
expect(files).toEqual(['test.yml']);
|
|
|
});
|
|
|
|
|
|
it('should not copy the folder if it already exists', () => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
- [`${getConfig().rootFolder}/apps/test`]: ['docker-compose.yml'],
|
|
|
- [`${getConfig().rootFolder}/apps/test/docker-compose.yml`]: 'test',
|
|
|
+ [`/runtipi/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
+ ['/app/storage/apps/test']: ['docker-compose.yml'],
|
|
|
+ ['/app/storage/apps/test/docker-compose.yml']: 'test',
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
@@ -193,15 +187,15 @@ describe('Test: ensureAppFolder', () => {
|
|
|
ensureAppFolder('test');
|
|
|
|
|
|
// Assert
|
|
|
- const files = fs.readdirSync(`${getConfig().rootFolder}/apps/test`);
|
|
|
+ const files = fs.readdirSync('/app/storage/apps/test');
|
|
|
expect(files).toEqual(['docker-compose.yml']);
|
|
|
});
|
|
|
|
|
|
it('Should overwrite the folder if clean up is true', () => {
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
- [`${getConfig().rootFolder}/apps/test`]: ['docker-compose.yml'],
|
|
|
- [`${getConfig().rootFolder}/apps/test/docker-compose.yml`]: 'test',
|
|
|
+ [`/runtipi/repos/${getConfig().appsRepoId}/apps/test`]: ['test.yml'],
|
|
|
+ ['/app/storage/apps/test']: ['docker-compose.yml'],
|
|
|
+ ['/app/storage/apps/test/docker-compose.yml']: 'test',
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
@@ -211,7 +205,7 @@ describe('Test: ensureAppFolder', () => {
|
|
|
ensureAppFolder('test', true);
|
|
|
|
|
|
// Assert
|
|
|
- const files = fs.readdirSync(`${getConfig().rootFolder}/apps/test`);
|
|
|
+ const files = fs.readdirSync('/app/storage/apps/test');
|
|
|
expect(files).toEqual(['test.yml']);
|
|
|
});
|
|
|
|
|
@@ -219,8 +213,8 @@ describe('Test: ensureAppFolder', () => {
|
|
|
// Arrange
|
|
|
const randomFileName = `${faker.random.word()}.yml`;
|
|
|
const mockFiles = {
|
|
|
- [`${getConfig().rootFolder}/repos/${getConfig().appsRepoId}/apps/test`]: [randomFileName],
|
|
|
- [`${getConfig().rootFolder}/apps/test`]: ['test.yml'],
|
|
|
+ [`/runtipi/repos/${getConfig().appsRepoId}/apps/test`]: [randomFileName],
|
|
|
+ ['/app/storage/apps/test']: ['test.yml'],
|
|
|
};
|
|
|
|
|
|
// @ts-ignore
|
|
@@ -230,7 +224,7 @@ describe('Test: ensureAppFolder', () => {
|
|
|
ensureAppFolder('test');
|
|
|
|
|
|
// Assert
|
|
|
- const files = fs.readdirSync(`${getConfig().rootFolder}/apps/test`);
|
|
|
+ const files = fs.readdirSync('/app/storage/apps/test');
|
|
|
expect(files).toEqual([randomFileName]);
|
|
|
});
|
|
|
});
|