Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make addon context-aware. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/git-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const wrench = require('wrench')
const temp = require('temp')
const _ = require('underscore')
const {it, fit, beforeEach} = require('./async-spec-helper-functions')
const {Worker} = require('worker_threads')

describe('git', () => {
let repo
Expand Down Expand Up @@ -1138,6 +1139,35 @@ describe('git', () => {
})
})

describe('Context Aware', () => {
it('should handle opening the same repository from different contexts', async () => {
// Based on it('returns the path to the .git directory')
let currentGitPath = path.join(path.dirname(__dirname), '.git/')
if (process.platform === 'win32') { currentGitPath = currentGitPath.replace(/\\/g, '/') }
const promises = [];
for (let i = 1; i <= 5; ++i) {
promises.push(new Promise((resolve, reject) => {
const worker = new Worker('./spec/worker.cjs');
let repositoryPath;
worker.on('message', function(message) {
repositoryPath = message;
});
worker.on('exit', function(code) {
if (code === 0) {
resolve(repositoryPath)
}
else {
reject(`Worker error ${code}`)
}
});
}))
}
const responses = await Promise.all(promises)
const c = currentGitPath;
expect(responses).toEqual([c, c, c, c, c])
});
})

function execCommands (commands, callback) {
let command
if (process.platform === 'win32') {
Expand Down
6 changes: 6 additions & 0 deletions spec/worker.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const parentPort = require('worker_threads').parentPort

const git = require('../src/git')

const repositoryPath = git.open(__dirname).getPath()
parentPort.postMessage(repositoryPath);
4 changes: 2 additions & 2 deletions src/repository.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <map>
#include <utility>

void Repository::Init(Local<Object> target) {
NAN_MODULE_INIT(Repository::Init) {
Nan::HandleScope scope;
git_libgit2_init();

Expand Down Expand Up @@ -68,7 +68,7 @@ void Repository::Init(Local<Object> target) {
Nan::GetFunction(newTemplate).ToLocalChecked());
}

NODE_MODULE(git, Repository::Init)
NAN_MODULE_WORKER_ENABLED(git, Repository::Init)

NAN_METHOD(Repository::New) {
Nan::HandleScope scope;
Expand Down
2 changes: 1 addition & 1 deletion src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace v8; // NOLINT

class Repository : public Nan::ObjectWrap {
public:
static void Init(Local<Object> target);
static NAN_MODULE_INIT(Init);

private:
static NAN_METHOD(New);
Expand Down