Skip to content

Commit

Permalink
add tmp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Dec 16, 2023
1 parent 312f2e4 commit d0b2758
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/axios.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: npm khala-axios
name: @davidkhala/axios
on: [workflow_dispatch, push]
defaults:
run:
Expand Down
2 changes: 1 addition & 1 deletion light/devOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ export const os = {
export const uid = OS.userInfo().uid;
export const hostname = OS.hostname();// 'localhost.localdomain';
export const tempdir = OS.tmpdir();// '/tmp'
export const homedir = OS.homedir();//
export const homedir = OS.homedir();
19 changes: 17 additions & 2 deletions light/test/devopsTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {consoleLogger} from '@davidkhala/logger/log4.js';
import assert from 'assert';
import {homeResolve} from '../path.js';
import {execStream, hostname, ip, tempdir, execSync, os, uid} from '../devOps.js';

const logger = consoleLogger('devops');
Expand All @@ -18,11 +19,25 @@ describe('devOps', function () {
it('execStream: npm i', async () => {
execStream('npm install');
});
it('os', async () => {
it('tempdir', () => {
logger.info({tempdir});
logger.info({hostname});
switch (os.platform) {
case 'win32':
assert.equal(tempdir, homeResolve('AppData', 'Local', 'Temp'));
break;
case 'linux':
assert.equal(tempdir, '/tmp');
}

});
it('ip', () => {
const address = ip(true);
assert.ok(Array.isArray(address));
});
it('hostname', () => {
logger.info({hostname});
});
it('os', () => {
logger.info(os);
});
it('uid', () => {
Expand Down
26 changes: 26 additions & 0 deletions main/test/tmpTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs';
import assert from 'assert';
import {createTmpFile, createTmpDir, randomName} from '../tmp.js';
import {isDirectory} from '@davidkhala/light/file.js';
import {tempdir} from '@davidkhala/light/devOps.js';

describe('test:npm tmp', function () {
this.timeout(0);
it('file create', () => {
const [name, cb] = createTmpFile();
assert.ok(fs.existsSync(name));
assert.ok(name.startsWith(tempdir));
cb();
});
it('directory create', () => {
const [name, cb] = createTmpDir();
assert.ok(isDirectory(name));
assert.ok(name.startsWith(tempdir));
cb();
});
it('generate name', () => {
const name = randomName();
assert.ok(!fs.existsSync(name));
assert.ok(name.startsWith(tempdir));
});
});
12 changes: 6 additions & 6 deletions main/tmp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Tmp from 'tmp';

Tmp.setGracefulCleanup();


Expand All @@ -9,9 +10,10 @@ Tmp.setGracefulCleanup();
*/
export const createTmpFile = (options) => {
const obj = Tmp.fileSync(options);
return [obj.name, () => {
obj.removeCallback();
}];
return [obj.name, obj.removeCallback];
};
export const randomName = (options) => {
return Tmp.tmpNameSync(options);
};
/**
*
Expand All @@ -20,7 +22,5 @@ export const createTmpFile = (options) => {
*/
export const createTmpDir = (options = {unsafeCleanup: true}) => {
const obj = Tmp.dirSync(options);
return [obj.name, () => {
obj.removeCallback();
}];
return [obj.name, obj.removeCallback];
};

0 comments on commit d0b2758

Please sign in to comment.