pulsar/spec/git-repository-spec.js

448 lines
14 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
const path = require('path');
const fs = require('fs-plus');
const temp = require('temp').track();
const GitRepository = require('../src/git-repository');
const Project = require('../src/project');
2017-11-06 21:25:31 +03:00
describe('GitRepository', () => {
2019-05-31 19:33:56 +03:00
let repo;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const gitPath = path.join(temp.dir, '.git');
if (fs.isDirectorySync(gitPath)) fs.removeSync(gitPath);
});
2017-11-06 21:25:31 +03:00
afterEach(() => {
2019-05-31 19:33:56 +03:00
if (repo && !repo.isDestroyed()) repo.destroy();
});
2017-11-06 21:25:31 +03:00
describe('@open(path)', () => {
it('returns null when no repository is found', () => {
2019-05-31 19:33:56 +03:00
expect(GitRepository.open(path.join(temp.dir, 'nogit.txt'))).toBeNull();
});
});
2017-11-06 21:25:31 +03:00
describe('new GitRepository(path)', () => {
it('throws an exception when no repository is found', () => {
2019-02-22 10:55:17 +03:00
expect(
() => new GitRepository(path.join(temp.dir, 'nogit.txt'))
2019-05-31 19:33:56 +03:00
).toThrow();
});
});
2017-11-06 21:25:31 +03:00
describe('.getPath()', () => {
it('returns the repository path for a .git directory path with a directory', () => {
2019-02-22 10:55:17 +03:00
repo = new GitRepository(
path.join(__dirname, 'fixtures', 'git', 'master.git', 'objects')
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
expect(repo.getPath()).toBe(
path.join(__dirname, 'fixtures', 'git', 'master.git')
2019-05-31 19:33:56 +03:00
);
});
2017-11-06 21:25:31 +03:00
it('returns the repository path for a repository path', () => {
2019-02-22 10:55:17 +03:00
repo = new GitRepository(
path.join(__dirname, 'fixtures', 'git', 'master.git')
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
expect(repo.getPath()).toBe(
path.join(__dirname, 'fixtures', 'git', 'master.git')
2019-05-31 19:33:56 +03:00
);
});
});
2017-11-06 21:25:31 +03:00
describe('.isPathIgnored(path)', () => {
it('returns true for an ignored path', () => {
2019-02-22 10:55:17 +03:00
repo = new GitRepository(
path.join(__dirname, 'fixtures', 'git', 'ignore.git')
2019-05-31 19:33:56 +03:00
);
expect(repo.isPathIgnored('a.txt')).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it('returns false for a non-ignored path', () => {
2019-02-22 10:55:17 +03:00
repo = new GitRepository(
path.join(__dirname, 'fixtures', 'git', 'ignore.git')
2019-05-31 19:33:56 +03:00
);
expect(repo.isPathIgnored('b.txt')).toBeFalsy();
});
});
2017-11-06 21:25:31 +03:00
describe('.isPathModified(path)', () => {
2019-05-31 19:33:56 +03:00
let filePath, newPath;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const workingDirPath = copyRepository();
repo = new GitRepository(workingDirPath);
filePath = path.join(workingDirPath, 'a.txt');
newPath = path.join(workingDirPath, 'new-path.txt');
});
2017-11-06 21:25:31 +03:00
describe('when the path is unstaged', () => {
it('returns false if the path has not been modified', () => {
2019-05-31 19:33:56 +03:00
expect(repo.isPathModified(filePath)).toBeFalsy();
});
2017-11-06 21:25:31 +03:00
it('returns true if the path is modified', () => {
2019-05-31 19:33:56 +03:00
fs.writeFileSync(filePath, 'change');
expect(repo.isPathModified(filePath)).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it('returns true if the path is deleted', () => {
2019-05-31 19:33:56 +03:00
fs.removeSync(filePath);
expect(repo.isPathModified(filePath)).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it('returns false if the path is new', () => {
2019-05-31 19:33:56 +03:00
expect(repo.isPathModified(newPath)).toBeFalsy();
});
});
});
2017-11-06 21:25:31 +03:00
describe('.isPathNew(path)', () => {
2019-05-31 19:33:56 +03:00
let filePath, newPath;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const workingDirPath = copyRepository();
repo = new GitRepository(workingDirPath);
filePath = path.join(workingDirPath, 'a.txt');
newPath = path.join(workingDirPath, 'new-path.txt');
fs.writeFileSync(newPath, "i'm new here");
});
2017-11-06 21:25:31 +03:00
describe('when the path is unstaged', () => {
it('returns true if the path is new', () => {
2019-05-31 19:33:56 +03:00
expect(repo.isPathNew(newPath)).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it("returns false if the path isn't new", () => {
2019-05-31 19:33:56 +03:00
expect(repo.isPathNew(filePath)).toBeFalsy();
});
});
});
2017-11-06 21:25:31 +03:00
describe('.checkoutHead(path)', () => {
2019-05-31 19:33:56 +03:00
let filePath;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const workingDirPath = copyRepository();
repo = new GitRepository(workingDirPath);
filePath = path.join(workingDirPath, 'a.txt');
});
2017-11-06 21:25:31 +03:00
it('no longer reports a path as modified after checkout', () => {
2019-05-31 19:33:56 +03:00
expect(repo.isPathModified(filePath)).toBeFalsy();
fs.writeFileSync(filePath, 'ch ch changes');
expect(repo.isPathModified(filePath)).toBeTruthy();
expect(repo.checkoutHead(filePath)).toBeTruthy();
expect(repo.isPathModified(filePath)).toBeFalsy();
});
2017-11-06 21:25:31 +03:00
it('restores the contents of the path to the original text', () => {
2019-05-31 19:33:56 +03:00
fs.writeFileSync(filePath, 'ch ch changes');
expect(repo.checkoutHead(filePath)).toBeTruthy();
expect(fs.readFileSync(filePath, 'utf8')).toBe('');
});
2017-11-06 21:25:31 +03:00
it('fires a status-changed event if the checkout completes successfully', () => {
2019-05-31 19:33:56 +03:00
fs.writeFileSync(filePath, 'ch ch changes');
repo.getPathStatus(filePath);
const statusHandler = jasmine.createSpy('statusHandler');
repo.onDidChangeStatus(statusHandler);
repo.checkoutHead(filePath);
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler.argsForCall[0][0]).toEqual({
path: filePath,
pathStatus: 0
2019-05-31 19:33:56 +03:00
});
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
repo.checkoutHead(filePath);
expect(statusHandler.callCount).toBe(1);
});
});
2017-11-06 21:25:31 +03:00
describe('.checkoutHeadForEditor(editor)', () => {
2019-05-31 19:33:56 +03:00
let filePath, editor;
2017-11-06 21:25:31 +03:00
beforeEach(async () => {
2019-05-31 19:33:56 +03:00
spyOn(atom, 'confirm');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
const workingDirPath = copyRepository();
2019-02-22 10:55:17 +03:00
repo = new GitRepository(workingDirPath, {
project: atom.project,
config: atom.config,
confirm: atom.confirm
2019-05-31 19:33:56 +03:00
});
filePath = path.join(workingDirPath, 'a.txt');
fs.writeFileSync(filePath, 'ch ch changes');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
editor = await atom.workspace.open(filePath);
});
2017-11-06 21:25:31 +03:00
it('displays a confirmation dialog by default', () => {
// Permissions issues with this test on Windows
2019-05-31 19:33:56 +03:00
if (process.platform === 'win32') return;
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
atom.confirm.andCallFake(({ buttons }) => buttons.OK());
atom.config.set('editor.confirmCheckoutHeadRevision', true);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
repo.checkoutHeadForEditor(editor);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
expect(fs.readFileSync(filePath, 'utf8')).toBe('');
});
2017-11-06 21:25:31 +03:00
it('does not display a dialog when confirmation is disabled', () => {
// Flakey EPERM opening a.txt on Win32
2019-05-31 19:33:56 +03:00
if (process.platform === 'win32') return;
atom.config.set('editor.confirmCheckoutHeadRevision', false);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
repo.checkoutHeadForEditor(editor);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
expect(fs.readFileSync(filePath, 'utf8')).toBe('');
expect(atom.confirm).not.toHaveBeenCalled();
});
});
2017-11-06 21:25:31 +03:00
describe('.destroy()', () => {
it('throws an exception when any method is called after it is called', () => {
2019-02-22 10:55:17 +03:00
repo = new GitRepository(
path.join(__dirname, 'fixtures', 'git', 'master.git')
2019-05-31 19:33:56 +03:00
);
repo.destroy();
expect(() => repo.getShortHead()).toThrow();
});
});
2017-11-06 21:25:31 +03:00
describe('.getPathStatus(path)', () => {
2019-05-31 19:33:56 +03:00
let filePath;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const workingDirectory = copyRepository();
repo = new GitRepository(workingDirectory);
filePath = path.join(workingDirectory, 'file.txt');
});
2017-11-06 21:25:31 +03:00
it('trigger a status-changed event when the new status differs from the last cached one', () => {
2019-05-31 19:33:56 +03:00
const statusHandler = jasmine.createSpy('statusHandler');
repo.onDidChangeStatus(statusHandler);
fs.writeFileSync(filePath, '');
let status = repo.getPathStatus(filePath);
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler.argsForCall[0][0]).toEqual({
path: filePath,
pathStatus: status
2019-05-31 19:33:56 +03:00
});
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
fs.writeFileSync(filePath, 'abc');
status = repo.getPathStatus(filePath);
expect(statusHandler.callCount).toBe(1);
});
});
2017-11-06 21:25:31 +03:00
describe('.getDirectoryStatus(path)', () => {
2019-05-31 19:33:56 +03:00
let directoryPath, filePath;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
const workingDirectory = copyRepository();
repo = new GitRepository(workingDirectory);
directoryPath = path.join(workingDirectory, 'dir');
filePath = path.join(directoryPath, 'b.txt');
});
2017-11-06 21:25:31 +03:00
it('gets the status based on the files inside the directory', () => {
2019-02-22 10:55:17 +03:00
expect(
repo.isStatusModified(repo.getDirectoryStatus(directoryPath))
2019-05-31 19:33:56 +03:00
).toBe(false);
fs.writeFileSync(filePath, 'abc');
repo.getPathStatus(filePath);
2019-02-22 10:55:17 +03:00
expect(
repo.isStatusModified(repo.getDirectoryStatus(directoryPath))
2019-05-31 19:33:56 +03:00
).toBe(true);
});
});
2017-11-06 21:25:31 +03:00
describe('.refreshStatus()', () => {
2019-05-31 19:33:56 +03:00
let newPath, modifiedPath, cleanPath, workingDirectory;
2017-11-06 21:25:31 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
workingDirectory = copyRepository();
2019-02-22 10:55:17 +03:00
repo = new GitRepository(workingDirectory, {
project: atom.project,
config: atom.config
2019-05-31 19:33:56 +03:00
});
modifiedPath = path.join(workingDirectory, 'file.txt');
newPath = path.join(workingDirectory, 'untracked.txt');
cleanPath = path.join(workingDirectory, 'other.txt');
fs.writeFileSync(cleanPath, 'Full of text');
fs.writeFileSync(newPath, '');
newPath = fs.absolute(newPath);
});
2017-11-06 21:25:31 +03:00
it('returns status information for all new and modified files', async () => {
2019-05-31 19:33:56 +03:00
const statusHandler = jasmine.createSpy('statusHandler');
repo.onDidChangeStatuses(statusHandler);
fs.writeFileSync(modifiedPath, 'making this path modified');
await repo.refreshStatus();
expect(statusHandler.callCount).toBe(1);
expect(repo.getCachedPathStatus(cleanPath)).toBeUndefined();
expect(repo.isStatusNew(repo.getCachedPathStatus(newPath))).toBeTruthy();
2019-02-22 10:55:17 +03:00
expect(
repo.isStatusModified(repo.getCachedPathStatus(modifiedPath))
2019-05-31 19:33:56 +03:00
).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it('caches the proper statuses when a subdir is open', async () => {
2019-05-31 19:33:56 +03:00
const subDir = path.join(workingDirectory, 'dir');
fs.mkdirSync(subDir);
const filePath = path.join(subDir, 'b.txt');
fs.writeFileSync(filePath, '');
atom.project.setPaths([subDir]);
await atom.workspace.open('b.txt');
repo = atom.project.getRepositories()[0];
await repo.refreshStatus();
const status = repo.getCachedPathStatus(filePath);
expect(repo.isStatusModified(status)).toBe(false);
expect(repo.isStatusNew(status)).toBe(false);
});
2017-11-06 21:25:31 +03:00
it('works correctly when the project has multiple folders (regression)', async () => {
2019-05-31 19:33:56 +03:00
atom.project.addPath(workingDirectory);
atom.project.addPath(path.join(__dirname, 'fixtures', 'dir'));
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
await repo.refreshStatus();
expect(repo.getCachedPathStatus(cleanPath)).toBeUndefined();
expect(repo.isStatusNew(repo.getCachedPathStatus(newPath))).toBeTruthy();
2019-02-22 10:55:17 +03:00
expect(
repo.isStatusModified(repo.getCachedPathStatus(modifiedPath))
2019-05-31 19:33:56 +03:00
).toBeTruthy();
});
2017-11-06 21:25:31 +03:00
it('caches statuses that were looked up synchronously', async () => {
2019-05-31 19:33:56 +03:00
const originalContent = 'undefined';
fs.writeFileSync(modifiedPath, 'making this path modified');
repo.getPathStatus('file.txt');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
fs.writeFileSync(modifiedPath, originalContent);
await repo.refreshStatus();
2019-02-22 10:55:17 +03:00
expect(
repo.isStatusModified(repo.getCachedPathStatus(modifiedPath))
2019-05-31 19:33:56 +03:00
).toBeFalsy();
});
});
2017-11-06 21:25:31 +03:00
describe('buffer events', () => {
2019-05-31 19:33:56 +03:00
let editor;
2017-11-06 21:25:31 +03:00
beforeEach(async () => {
2019-05-31 19:33:56 +03:00
atom.project.setPaths([copyRepository()]);
2019-02-22 10:55:17 +03:00
const refreshPromise = new Promise(resolve =>
atom.project.getRepositories()[0].onDidChangeStatuses(resolve)
2019-05-31 19:33:56 +03:00
);
editor = await atom.workspace.open('other.txt');
await refreshPromise;
});
2017-11-06 21:25:31 +03:00
it('emits a status-changed event when a buffer is saved', async () => {
2019-05-31 19:33:56 +03:00
editor.insertNewline();
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
const statusHandler = jasmine.createSpy('statusHandler');
atom.project.getRepositories()[0].onDidChangeStatus(statusHandler);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
await editor.save();
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler).toHaveBeenCalledWith({
path: editor.getPath(),
pathStatus: 256
2019-05-31 19:33:56 +03:00
});
});
2017-11-06 21:25:31 +03:00
it('emits a status-changed event when a buffer is reloaded', async () => {
2019-05-31 19:33:56 +03:00
fs.writeFileSync(editor.getPath(), 'changed');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
const statusHandler = jasmine.createSpy('statusHandler');
atom.project.getRepositories()[0].onDidChangeStatus(statusHandler);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
await editor.getBuffer().reload();
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler).toHaveBeenCalledWith({
path: editor.getPath(),
pathStatus: 256
2019-05-31 19:33:56 +03:00
});
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
await editor.getBuffer().reload();
expect(statusHandler.callCount).toBe(1);
});
2017-11-06 21:25:31 +03:00
it("emits a status-changed event when a buffer's path changes", () => {
2019-05-31 19:33:56 +03:00
fs.writeFileSync(editor.getPath(), 'changed');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
const statusHandler = jasmine.createSpy('statusHandler');
atom.project.getRepositories()[0].onDidChangeStatus(statusHandler);
editor.getBuffer().emitter.emit('did-change-path');
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler).toHaveBeenCalledWith({
path: editor.getPath(),
pathStatus: 256
2019-05-31 19:33:56 +03:00
});
editor.getBuffer().emitter.emit('did-change-path');
expect(statusHandler.callCount).toBe(1);
});
2017-11-06 21:25:31 +03:00
it('stops listening to the buffer when the repository is destroyed (regression)', () => {
2019-05-31 19:33:56 +03:00
atom.project.getRepositories()[0].destroy();
expect(() => editor.save()).not.toThrow();
});
});
2017-11-06 21:25:31 +03:00
describe('when a project is deserialized', () => {
2019-05-31 19:33:56 +03:00
let buffer, project2, statusHandler;
2017-11-06 21:25:31 +03:00
afterEach(() => {
2019-05-31 19:33:56 +03:00
if (project2) project2.destroy();
});
2017-11-06 21:25:31 +03:00
it('subscribes to all the serialized buffers in the project', async () => {
2019-05-31 19:33:56 +03:00
atom.project.setPaths([copyRepository()]);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
await atom.workspace.open('file.txt');
2017-11-06 21:25:31 +03:00
project2 = new Project({
notificationManager: atom.notifications,
packageManager: atom.packages,
confirm: atom.confirm,
grammarRegistry: atom.grammars,
applicationDelegate: atom.applicationDelegate
2019-05-31 19:33:56 +03:00
});
await project2.deserialize(
atom.project.serialize({ isUnloading: false })
);
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
buffer = project2.getBuffers()[0];
buffer.append('changes');
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
statusHandler = jasmine.createSpy('statusHandler');
project2.getRepositories()[0].onDidChangeStatus(statusHandler);
await buffer.save();
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
expect(statusHandler.callCount).toBe(1);
2019-02-22 10:55:17 +03:00
expect(statusHandler).toHaveBeenCalledWith({
path: buffer.getPath(),
pathStatus: 256
2019-05-31 19:33:56 +03:00
});
});
});
});
2017-11-06 21:25:31 +03:00
2019-05-31 19:33:56 +03:00
function copyRepository() {
const workingDirPath = temp.mkdirSync('atom-spec-git');
2019-02-22 10:55:17 +03:00
fs.copySync(
path.join(__dirname, 'fixtures', 'git', 'working-dir'),
workingDirPath
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
fs.renameSync(
path.join(workingDirPath, 'git.git'),
path.join(workingDirPath, '.git')
2019-05-31 19:33:56 +03:00
);
return workingDirPath;
2017-11-06 21:25:31 +03:00
}