mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-21 02:01:35 +00:00
Create touch
and getBootTime
utility functions
Change-type: patch
This commit is contained in:
parent
4e2b959481
commit
a4d91d381a
@ -3,6 +3,7 @@ import { promises as fs } from 'fs';
|
||||
import * as path from 'path';
|
||||
import { exec as execSync } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import { uptime } from 'os';
|
||||
|
||||
import * as constants from './constants';
|
||||
|
||||
@ -82,3 +83,26 @@ export async function unlinkAll(...paths: string[]): Promise<void> {
|
||||
export function getPathOnHost(...paths: string[]): string[] {
|
||||
return paths.map((p: string) => path.join(constants.rootMountPoint, p));
|
||||
}
|
||||
|
||||
/**
|
||||
* Change modification and access time of the given file.
|
||||
* It creates an empty file if it does not exist
|
||||
*/
|
||||
export const touch = (file: string, time = new Date()) =>
|
||||
// set both access time and modified time to the value passed
|
||||
// as argument (default to `now`)
|
||||
fs.utimes(file, time, time).catch((e) =>
|
||||
// only create the file if it doesn't exist,
|
||||
// if some other error happens is probably better to not touch it
|
||||
e.code === 'ENOENT'
|
||||
? fs
|
||||
.open(file, 'w')
|
||||
.then((fd) => fd.close())
|
||||
// If date is custom we need to change the file atime and mtime
|
||||
.then(() => fs.utimes(file, time, time))
|
||||
: e,
|
||||
);
|
||||
|
||||
// Get the system boot time as a Date object
|
||||
export const getBootTime = () =>
|
||||
new Date(new Date().getTime() - uptime() * 1000);
|
||||
|
@ -15,8 +15,14 @@ describe('lib/fs-utils', () => {
|
||||
|
||||
const mockFs = () => {
|
||||
mock({
|
||||
[testFile1]: 'foo',
|
||||
[testFile2]: 'bar',
|
||||
[testFile1]: mock.file({
|
||||
content: 'foo',
|
||||
mtime: new Date('2022-01-04T00:00:00'),
|
||||
}),
|
||||
[testFile2]: mock.file({
|
||||
content: 'bar',
|
||||
mtime: new Date('2022-01-04T00:00:00'),
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
@ -154,4 +160,41 @@ describe('lib/fs-utils', () => {
|
||||
).to.deep.equal([testFile1, testFile2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('touch', () => {
|
||||
beforeEach(mockFs);
|
||||
afterEach(unmockFs);
|
||||
|
||||
it('creates the file if it does not exist', async () => {
|
||||
await fsUtils.touch('somefile');
|
||||
expect(await fsUtils.exists('somefile')).to.be.true;
|
||||
});
|
||||
|
||||
it('updates the file mtime if file already exists', async () => {
|
||||
const statsBefore = await fs.stat(testFile1);
|
||||
await fsUtils.touch(testFile1);
|
||||
const statsAfter = await fs.stat(testFile1);
|
||||
|
||||
// Mtime should be different
|
||||
expect(statsAfter.mtime.getTime()).to.not.equal(
|
||||
statsBefore.mtime.getTime(),
|
||||
);
|
||||
});
|
||||
|
||||
it('allows setting a custom time for existing files', async () => {
|
||||
const customTime = new Date('1981-11-24T12:00:00');
|
||||
await fsUtils.touch(testFile1, customTime);
|
||||
const statsAfter = await fs.stat(testFile1);
|
||||
|
||||
expect(statsAfter.mtime.getTime()).to.be.equal(customTime.getTime());
|
||||
});
|
||||
|
||||
it('allows setting a custom time for newly created files', async () => {
|
||||
const customTime = new Date('1981-11-24T12:00:00');
|
||||
await fsUtils.touch('somefile', customTime);
|
||||
const statsAfter = await fs.stat('somefile');
|
||||
|
||||
expect(statsAfter.mtime.getTime()).to.be.equal(customTime.getTime());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user