Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Test coverage for GitTempDir #1901

Merged
merged 1 commit into from Jan 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions test/git-temp-dir.test.js
@@ -0,0 +1,71 @@
import fs from 'fs-extra';
import path from 'path';

import GitTempDir, {BIN_SCRIPTS} from '../lib/git-temp-dir';

describe('GitTempDir', function() {
it('ensures that a temporary directory is populated', async function() {
const tempDir = new GitTempDir();
await tempDir.ensure();

const root = tempDir.getRootPath();
for (const scriptName in BIN_SCRIPTS) {
const script = BIN_SCRIPTS[scriptName];
const stat = await fs.stat(path.join(root, script));
assert.isTrue(stat.isFile());
if (script.endsWith('.sh') && process.platform !== 'win32') {
// eslint-disable-next-line no-bitwise
assert.isTrue((stat.mode & fs.constants.S_IXUSR) === fs.constants.S_IXUSR);
}
}

await tempDir.ensure();
assert.strictEqual(root, tempDir.getRootPath());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for my own education...

you assign root = tempDir.getRootPath on line 11, and then assert that that these are still equal. With this assertion, are you testing that ensure() does not change the root path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this assertion, are you testing that ensure() does not change the root path?

Yup, exactly. I wanted to assert that multiple calls to ensure() wouldn't create new temp directories, basically.

});

it('generates getters for script paths', async function() {
const tempDir = new GitTempDir();
await tempDir.ensure();

const scriptPath = tempDir.getScriptPath('git-credential-atom.js');
assert.isTrue(scriptPath.startsWith(tempDir.getRootPath()));
assert.isTrue(scriptPath.endsWith('git-credential-atom.js'));

assert.strictEqual(tempDir.getCredentialHelperJs(), tempDir.getScriptPath('git-credential-atom.js'));
assert.strictEqual(tempDir.getCredentialHelperSh(), tempDir.getScriptPath('git-credential-atom.sh'));
assert.strictEqual(tempDir.getAskPassJs(), tempDir.getScriptPath('git-askpass-atom.js'));
});

it('fails when the temp dir is not yet created', function() {
const tempDir = new GitTempDir();
assert.throws(() => tempDir.getAskPassJs(), /uninitialized GitTempDir/);
});

if (process.platform === 'win32') {
it('generates a valid named pipe path for its socket', async function() {
const tempDir = new GitTempDir();
await tempDir.ensure();

assert.match(tempDir.getSocketPath(), /^\\\\\?\\pipe\\/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun regex. oh, windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named pipes are fun 🙃

});
} else {
it('generates a socket path within the directory', async function() {
const tempDir = new GitTempDir();
await tempDir.ensure();

assert.isTrue(tempDir.getSocketPath().startsWith(tempDir.getRootPath()));
});
}

it('deletes the directory on dispose', async function() {
const tempDir = new GitTempDir();
await tempDir.ensure();

const beforeStat = await fs.stat(tempDir.getRootPath());
assert.isTrue(beforeStat.isDirectory());

await tempDir.dispose();

await assert.isRejected(fs.stat(tempDir.getRootPath()), /ENOENT/);
});
});