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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2017, 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.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  18. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  19. import org.eclipse.jgit.pgm.opt.SubcommandHandler;
  20. import org.junit.Test;
  21. import org.kohsuke.args4j.Argument;
  22. public class TextBuiltinTest extends CLIRepositoryTestCase {
  23. public static class GitCliJGitWrapperParser {
  24. @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
  25. TextBuiltin subcommand;
  26. @Argument(index = 1, metaVar = "metaVar_arg")
  27. List<String> arguments = new ArrayList<>();
  28. }
  29. private String[] runAndCaptureUsingInitRaw(String... args)
  30. throws Exception {
  31. CLIGitCommand.Result result = new CLIGitCommand.Result();
  32. GitCliJGitWrapperParser bean = new GitCliJGitWrapperParser();
  33. final CmdLineParser clp = new CmdLineParser(bean);
  34. clp.parseArgument(args);
  35. final TextBuiltin cmd = bean.subcommand;
  36. cmd.initRaw(db, null, null, result.out, result.err);
  37. cmd.execute(bean.arguments.toArray(new String[bean.arguments.size()]));
  38. if (cmd.getOutputWriter() != null) {
  39. cmd.getOutputWriter().flush();
  40. }
  41. if (cmd.getErrorWriter() != null) {
  42. cmd.getErrorWriter().flush();
  43. }
  44. return result.outLines().toArray(new String[0]);
  45. }
  46. @Test
  47. public void testCleanDeleteDirs() throws Exception {
  48. try (Git git = new Git(db)) {
  49. git.commit().setMessage("initial commit").call();
  50. writeTrashFile("dir/file", "someData");
  51. writeTrashFile("a", "someData");
  52. writeTrashFile("b", "someData");
  53. // all these files should be there
  54. assertTrue(check(db, "a"));
  55. assertTrue(check(db, "b"));
  56. assertTrue(check(db, "dir/file"));
  57. assertArrayOfLinesEquals(new String[] { "Removing a", "Removing b",
  58. "Removing dir/" },
  59. runAndCaptureUsingInitRaw("clean", "-d", "-f"));
  60. assertFalse(check(db, "a"));
  61. assertFalse(check(db, "b"));
  62. assertFalse(check(db, "dir/file"));
  63. }
  64. }
  65. }