|
| 1 | +import DiscardHistory from '../../lib/models/discard-history'; |
| 2 | +import Repository from '../../lib/models/repository'; |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import { |
| 6 | + cloneRepository, setUpLocalAndRemoteRepositories, getHeadCommitOnRemote, |
| 7 | + assertDeepPropertyVals, assertEqualSortedArraysByKey, FAKE_USER, wireUpObserver, expectEvents, |
| 8 | +} from '../helpers'; |
| 9 | +import { formatWithOptions } from 'util'; |
| 10 | + |
| 11 | +describe('DiscardHistory', () => { |
| 12 | + describe('restoreLastDiscardInTempFiles', () => { |
| 13 | + let repository; |
| 14 | + |
| 15 | + it('Should undo discard of files with same name', async function() { |
| 16 | + // Setup |
| 17 | + const workdir = await cloneRepository('three-files'); |
| 18 | + repository = new Repository(workdir); |
| 19 | + await repository.getLoadPromise(); |
| 20 | + // Create 2 files with same name |
| 21 | + fs.mkdirSync(path.join(workdir, 'sub1')); |
| 22 | + fs.mkdirSync(path.join(workdir, 'sub2')); |
| 23 | + const file1 = path.join(workdir, 'sub1', 'a.txt') |
| 24 | + const file2 = path.join(workdir, 'sub2', 'a.txt'); |
| 25 | + fs.writeFileSync(file1, 'foo1\n', {encoding: 'utf8'}); |
| 26 | + fs.writeFileSync(file2, 'foo2\n', {encoding: 'utf8'}); |
| 27 | + |
| 28 | + console.log('New file created'); |
| 29 | + await repository.updateDiscardHistory(); |
| 30 | + const isSafe = function() { return true; }; |
| 31 | + const unstagedChanges = await repository.getUnstagedChanges(); |
| 32 | + const files = ['sub2/a.txt', 'sub1/a.txt']; |
| 33 | + await repository.storeBeforeAndAfterBlobs(files, isSafe, () => { |
| 34 | + files.pop(); |
| 35 | + files.pop(); |
| 36 | + }); |
| 37 | + |
| 38 | + // Discard changes and restore last discard |
| 39 | + await repository.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath)); |
| 40 | + const results = await repository.restoreLastDiscardInTempFiles(isSafe, null); |
| 41 | + |
| 42 | + // Check results |
| 43 | + results.map((merge_result, i) => { |
| 44 | + const {filePath, resultPath, conflict} = merge_result; |
| 45 | + if(path.dirname(filePath).endsWith('1')) |
| 46 | + assert.equal(fs.readFileSync(resultPath, 'utf8'), 'foo1\n'); |
| 47 | + else |
| 48 | + assert.equal(fs.readFileSync(resultPath, 'utf8'), 'foo2\n'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + }); |
| 53 | + |
| 54 | + describe('expandBlobsToFilesInTempFolder', () => { |
| 55 | + |
| 56 | + it('Should produce unique resultPaths', async () => { |
| 57 | + const history = new DiscardHistory(null, () => { |
| 58 | + return null; |
| 59 | + }, null, null); |
| 60 | + // simulate snapshots |
| 61 | + const snapshots = [ |
| 62 | + {filePath: 'subfolder1/file', beforeSha: 'beforeSha', afterSha: 'afterSha'}, |
| 63 | + {filePath: 'subfolder2/file', beforeSha: 'beforeSha', afterSha: 'afterSha'}, |
| 64 | + ]; |
| 65 | + |
| 66 | + // Perform test |
| 67 | + const results = await history.expandBlobsToFilesInTempFolder(snapshots); |
| 68 | + |
| 69 | + // Check results |
| 70 | + assert.isFalse(results[0].resultPath === results[1].resultPath); |
| 71 | + }); |
| 72 | + |
| 73 | + }); |
| 74 | +}); |
0 commit comments