You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2016, Ned Twigg <ned.twigg@diffplug.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import static org.eclipse.jgit.junit.JGitTestUtil.check;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  17. import org.junit.Test;
  18. public class CleanTest extends CLIRepositoryTestCase {
  19. @Test
  20. public void testCleanRequiresForce() throws Exception {
  21. try (Git git = new Git(db)) {
  22. assertArrayOfLinesEquals(
  23. new String[] { "Removing a", "Removing b" },
  24. execute("git clean"));
  25. } catch (Die e) {
  26. // TODO: should be "fatal: clean.requireForce defaults to true and
  27. // neither -i, -n, nor -f given; refusing to clean" but we don't
  28. // support -i yet. Fix this when/if we add support for -i.
  29. assertEquals(
  30. "fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean",
  31. e.getMessage());
  32. }
  33. }
  34. @Test
  35. public void testCleanRequiresForceConfig() throws Exception {
  36. try (Git git = new Git(db)) {
  37. git.getRepository().getConfig().setBoolean("clean", null,
  38. "requireForce", false);
  39. assertArrayOfLinesEquals(
  40. new String[] { "" },
  41. execute("git clean"));
  42. }
  43. }
  44. @Test
  45. public void testCleanLeaveDirs() throws Exception {
  46. try (Git git = new Git(db)) {
  47. git.commit().setMessage("initial commit").call();
  48. writeTrashFile("dir/file", "someData");
  49. writeTrashFile("a", "someData");
  50. writeTrashFile("b", "someData");
  51. // all these files should be there
  52. assertTrue(check(db, "a"));
  53. assertTrue(check(db, "b"));
  54. assertTrue(check(db, "dir/file"));
  55. // dry run should make no change
  56. assertArrayOfLinesEquals(
  57. new String[] { "Removing a", "Removing b" },
  58. execute("git clean -n"));
  59. assertTrue(check(db, "a"));
  60. assertTrue(check(db, "b"));
  61. assertTrue(check(db, "dir/file"));
  62. // force should make a change
  63. assertArrayOfLinesEquals(
  64. new String[] { "Removing a", "Removing b" },
  65. execute("git clean -f"));
  66. assertFalse(check(db, "a"));
  67. assertFalse(check(db, "b"));
  68. assertTrue(check(db, "dir/file"));
  69. }
  70. }
  71. @Test
  72. public void testCleanDeleteDirs() throws Exception {
  73. try (Git git = new Git(db)) {
  74. git.commit().setMessage("initial commit").call();
  75. writeTrashFile("dir/file", "someData");
  76. writeTrashFile("a", "someData");
  77. writeTrashFile("b", "someData");
  78. // all these files should be there
  79. assertTrue(check(db, "a"));
  80. assertTrue(check(db, "b"));
  81. assertTrue(check(db, "dir/file"));
  82. assertArrayOfLinesEquals(
  83. new String[] { "Removing a", "Removing b",
  84. "Removing dir/" },
  85. execute("git clean -d -f"));
  86. assertFalse(check(db, "a"));
  87. assertFalse(check(db, "b"));
  88. assertFalse(check(db, "dir/file"));
  89. }
  90. }
  91. }