Search in sources :

Example 11 with TestProject

use of org.eclipse.egit.core.test.TestProject in project egit by eclipse.

the class GitMoveDeleteHookTest method dotestMoveProjectWithinRepo.

private void dotestMoveProjectWithinRepo(String srcParent, String srcProjectName, String dstParent, String dstProjecName, String gitDir, boolean sourceInsideWs, boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    String gdRelativeSrcParent = srcParent + srcProjectName + "/";
    if (gdRelativeSrcParent.startsWith(gitDir))
        gdRelativeSrcParent = gdRelativeSrcParent.substring(gitDir.length());
    testDirs.add(new File(dstParent));
    String gdRelativeDstParent = dstParent + dstProjecName + "/";
    if (gdRelativeDstParent.startsWith(gitDir))
        gdRelativeDstParent = gdRelativeDstParent.substring(gitDir.length());
    registerWorkspaceRelativeTestDirProject(srcParent, srcProjectName);
    registerWorkspaceRelativeTestDirProject(dstParent, dstProjecName);
    // Old cruft may be laying around
    TestProject project = initRepoAboveProject(srcParent, gitDir, sourceInsideWs);
    IProject project0 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    project0.delete(true, null);
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);
    // Check condition before move
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry(gdRelativeSrcParent + "file.txt"));
    ObjectId oldContentId = dirCache.getEntry(gdRelativeSrcParent + "file.txt").getObjectId();
    // Modify the content before the move, we want to see the staged content
    // as it was before the move in the index
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    IProjectDescription description = project.getProject().getDescription();
    description.setName(dstProjecName);
    if (sourceInsideWs)
        if (dstParent.length() > 0)
            description.setLocationURI(URIUtil.toURI(project.getProject().getWorkspace().getRoot().getLocation().append(dstParent + dstProjecName)));
        else
            description.setLocationURI(null);
    else
        description.setLocationURI(URIUtil.toURI(new Path(workspaceSupplement + "/" + dstParent + "/" + dstProjecName)));
    project.getProject().move(description, IResource.FORCE | IResource.SHALLOW, null);
    IProject project2 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    assertTrue(project2.exists());
    assertNotNull(RepositoryMapping.getMapping(project2));
    // Check that our file exists on disk has a new location in the index
    dirCache.read();
    assertTrue(project2.getFile("file.txt").exists());
    if (autoStageMoves) {
        assertNotNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));
        // Same content in index as before the move, i.e. not same as on
        // disk
        assertEquals(oldContentId, dirCache.getEntry(gdRelativeDstParent + "file.txt").getObjectId());
    } else {
        assertNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DirCache(org.eclipse.jgit.dircache.DirCache) TestProject(org.eclipse.egit.core.test.TestProject) ObjectId(org.eclipse.jgit.lib.ObjectId) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

Example 12 with TestProject

use of org.eclipse.egit.core.test.TestProject in project egit by eclipse.

the class GitMoveDeleteHookTest method testMoveFolderWithFileWithConflictsShouldBeCanceled.

@Theory
public void testMoveFolderWithFileWithConflictsShouldBeCanceled(boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    String filePath = "folder/file.txt";
    IFile file = testUtils.addFileToProject(project.getProject(), filePath, "some text");
    Repository repo = testRepository.getRepository();
    DirCache index = repo.lockDirCache();
    DirCacheBuilder builder = index.builder();
    addUnmergedEntry(filePath, builder);
    builder.commit();
    try {
        project.getProject().getFolder("folder").move(project.getProject().getFolder("newfolder").getFullPath(), false, null);
        fail("Expected move of folder with file with conflicts to fail.");
    } catch (CoreException e) {
        IStatus status = e.getStatus();
        assertNotNull(status);
        assertEquals(IStatus.WARNING, status.getSeverity());
    }
    assertTrue("File should still exist at old location", file.exists());
    DirCache indexAfter = repo.readDirCache();
    DirCacheEntry entry = indexAfter.getEntry(filePath);
    assertEquals("Expected entry to still be in non-zero (conflict) stage", DirCacheEntry.STAGE_1, entry.getStage());
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) TestRepository(org.eclipse.egit.core.test.TestRepository) Repository(org.eclipse.jgit.lib.Repository) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) TestProject(org.eclipse.egit.core.test.TestProject) CoreException(org.eclipse.core.runtime.CoreException) Theory(org.junit.experimental.theories.Theory)

Example 13 with TestProject

use of org.eclipse.egit.core.test.TestProject in project egit by eclipse.

the class GitMoveDeleteHookTest method initRepoInsideProjectOutsideWorkspace.

private TestProject initRepoInsideProjectOutsideWorkspace() throws Exception {
    TestProject project = new TestProject(true, "Project-1", false, workspaceSupplement);
    File gitDir = new File(project.getProject().getLocationURI().getPath(), Constants.DOT_GIT);
    testDirs.add(gitDir);
    testRepository = new TestRepository(gitDir);
    repository = testRepository.getRepository();
    testRepository.connect(project.getProject());
    return project;
}
Also used : TestRepository(org.eclipse.egit.core.test.TestRepository) TestProject(org.eclipse.egit.core.test.TestProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 14 with TestProject

use of org.eclipse.egit.core.test.TestProject in project egit by eclipse.

the class GitMoveDeleteHookTest method testMoveFileWithConflictsShouldBeCanceled.

@Theory
public void testMoveFileWithConflictsShouldBeCanceled(boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    String filePath = "file.txt";
    IFile file = testUtils.addFileToProject(project.getProject(), filePath, "some text");
    Repository repo = testRepository.getRepository();
    DirCache index = repo.lockDirCache();
    DirCacheBuilder builder = index.builder();
    addUnmergedEntry(filePath, builder);
    builder.commit();
    try {
        file.move(new Path("destination.txt"), false, null);
        fail("Expected move of file with conflicts to fail.");
    } catch (CoreException e) {
        IStatus status = e.getStatus();
        assertNotNull(status);
        assertEquals(IStatus.WARNING, status.getSeverity());
    }
    assertTrue("File should still exist at old location", file.exists());
    DirCache indexAfter = repo.readDirCache();
    DirCacheEntry entry = indexAfter.getEntry(filePath);
    assertEquals("Expected entry to still be in non-zero (conflict) stage", DirCacheEntry.STAGE_1, entry.getStage());
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DirCache(org.eclipse.jgit.dircache.DirCache) TestRepository(org.eclipse.egit.core.test.TestRepository) Repository(org.eclipse.jgit.lib.Repository) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) TestProject(org.eclipse.egit.core.test.TestProject) CoreException(org.eclipse.core.runtime.CoreException) Theory(org.junit.experimental.theories.Theory)

Example 15 with TestProject

use of org.eclipse.egit.core.test.TestProject in project egit by eclipse.

the class GitMoveDeleteHookTest method testMoveAndRenameProjectContainingGitRepo.

/**
 * Rename and move a project in the workspace containing a Git repository.
 * <p>
 * The repository will be moved with the project.
 * Note that there is no way to rename a project in the workspace without
 * moving it. See https://bugs.eclipse.org/358828 for a discussion.
 *
 * @throws Exception
 */
@Test
public void testMoveAndRenameProjectContainingGitRepo() throws Exception {
    ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
    ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);
    IProjectDescription description = project.getProject().getDescription();
    description.setName("P2");
    registerWorkspaceRelativeTestDir("P2");
    project.getProject().move(description, IResource.FORCE | IResource.SHALLOW, null);
    IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject("P2");
    assertNotNull(RepositoryMapping.getMapping(project2.getProject()));
    Repository movedRepo = RepositoryMapping.getMapping(project2).getRepository();
    assertEquals("P2", movedRepo.getDirectory().getParentFile().getName());
    DirCache dc = movedRepo.readDirCache();
    assertEquals(1, dc.getEntryCount());
    assertEquals("file.txt", dc.getEntry(0).getPathString());
    assertFalse(ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").exists());
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) TestRepository(org.eclipse.egit.core.test.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestProject(org.eclipse.egit.core.test.TestProject) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IProject(org.eclipse.core.resources.IProject) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Aggregations

TestProject (org.eclipse.egit.core.test.TestProject)27 IFile (org.eclipse.core.resources.IFile)17 Test (org.junit.Test)17 TestRepository (org.eclipse.egit.core.test.TestRepository)11 DirCache (org.eclipse.jgit.dircache.DirCache)11 File (java.io.File)9 AddToIndexOperation (org.eclipse.egit.core.op.AddToIndexOperation)9 IProject (org.eclipse.core.resources.IProject)8 IPath (org.eclipse.core.runtime.IPath)8 Repository (org.eclipse.jgit.lib.Repository)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Theory (org.junit.experimental.theories.Theory)5 IProjectDescription (org.eclipse.core.resources.IProjectDescription)4 Path (org.eclipse.core.runtime.Path)4 ConnectProviderOperation (org.eclipse.egit.core.op.ConnectProviderOperation)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Ignore (org.junit.Ignore)3 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 IResource (org.eclipse.core.resources.IResource)2