Skip to content

Commit

Permalink
Do not store empty edit sessions (#153599)
Browse files Browse the repository at this point in the history
* Do not store empty edit sessions

* Add unit test
  • Loading branch information
joyceerhl authored Jun 28, 2022
1 parent 2ade948 commit 8fd6e38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon
if (uri === undefined) { return; }

// Run the store action to get back a ref
const ref = await that.storeEditSession();
const ref = await that.storeEditSession(false);

// Append the ref to the URI
if (ref !== undefined) {
Expand Down Expand Up @@ -215,7 +215,7 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon
await that.progressService.withProgress({
location: ProgressLocation.Notification,
title: localize('storing edit session', 'Storing edit session...')
}, async () => await that.storeEditSession());
}, async () => await that.storeEditSession(true));
}
}));
}
Expand Down Expand Up @@ -293,8 +293,9 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon
}
}

async storeEditSession(): Promise<string | undefined> {
async storeEditSession(fromStoreCommand: boolean): Promise<string | undefined> {
const folders: Folder[] = [];
let hasEdits = false;

for (const repository of this.scmService.repositories) {
// Look through all resource groups and compute which files were added/modified/deleted
Expand All @@ -321,6 +322,8 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon
}
} catch { }

hasEdits = true;

if (await this.fileService.exists(uri)) {
workingChanges.push({ type: ChangeType.Addition, fileType: FileType.File, contents: (await this.fileService.readFile(uri)).value.toString(), relativeFilePath: relativeFilePath });
} else {
Expand All @@ -332,6 +335,14 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon
folders.push({ workingChanges, name: name ?? '' });
}

if (!hasEdits) {
this.logService.info('Edit Sessions: Skipping storing edit session as there are no edits to store.');
if (fromStoreCommand) {
this.notificationService.info(localize('no edits to store', 'Skipped storing edit session as there are no edits to store.'));
}
return undefined;
}

const data: EditSession = { folders, version: 1 };

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ suite('Edit session sync', () => {
let instantiationService: TestInstantiationService;
let sessionSyncContribution: SessionSyncContribution;
let fileService: FileService;
let sandbox: sinon.SinonSandbox;

const disposables = new DisposableStore();

setup(() => {
suiteSetup(() => {
sandbox = sinon.createSandbox();

instantiationService = new TestInstantiationService();

// Set up filesystem
Expand Down Expand Up @@ -71,6 +74,9 @@ suite('Edit session sync', () => {
}
});

// Stub repositories
instantiationService.stub(ISCMService, '_repositories', new Map());

sessionSyncContribution = instantiationService.createInstance(SessionSyncContribution);
});

Expand Down Expand Up @@ -100,13 +106,9 @@ suite('Edit session sync', () => {
};

// Stub sync service to return edit session data
const sandbox = sinon.createSandbox();
const readStub = sandbox.stub().returns({ editSession, ref: '0' });
instantiationService.stub(ISessionSyncWorkbenchService, 'read', readStub);

// Stub repositories
instantiationService.stub(ISCMService, '_repositories', new Map());

// Create root folder
await fileService.createFolder(folderUri);

Expand All @@ -116,4 +118,17 @@ suite('Edit session sync', () => {
// Verify edit session was correctly applied
assert.equal((await fileService.readFile(fileUri)).value.toString(), fileContents);
});

test('Edit session not stored if there are no edits', async function () {
const writeStub = sandbox.stub();
instantiationService.stub(ISessionSyncWorkbenchService, 'write', writeStub);

// Create root folder
await fileService.createFolder(folderUri);

await sessionSyncContribution.storeEditSession(true);

// Verify that we did not attempt to write the edit session
assert.equal(writeStub.called, false);
});
});

0 comments on commit 8fd6e38

Please sign in to comment.