From 9f9b0b5d8f3f68e2e75000baa17fa2cb7783d250 Mon Sep 17 00:00:00 2001 From: slampy97 Date: Sun, 28 Mar 2021 06:35:04 +0300 Subject: [PATCH 1/3] 2 commands + tests for them --- src/main/java/commandline/commands/Cd.java | 18 ++++++++ src/main/java/commandline/commands/Ls.java | 34 +++++++++++++++ .../commandline/utils/CommandBuilder.java | 32 +++++++------- .../java/commandline/CommandLineTest.java | 42 ++++++++++++++++--- 4 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 src/main/java/commandline/commands/Cd.java create mode 100644 src/main/java/commandline/commands/Ls.java diff --git a/src/main/java/commandline/commands/Cd.java b/src/main/java/commandline/commands/Cd.java new file mode 100644 index 0000000..79a099b --- /dev/null +++ b/src/main/java/commandline/commands/Cd.java @@ -0,0 +1,18 @@ +package commandline.commands; + +import java.io.File; +import java.util.List; + +public class Cd implements Command { + @Override + public String run(List args, String before) { + if (args.isEmpty()) { + System.setProperty("user.dir", System.getProperty("user.home")); + } else { + File file = new File(args.get(0)); + System.setProperty("user.dir", file.getAbsolutePath()); + } + return null; + } + +} diff --git a/src/main/java/commandline/commands/Ls.java b/src/main/java/commandline/commands/Ls.java new file mode 100644 index 0000000..2d49374 --- /dev/null +++ b/src/main/java/commandline/commands/Ls.java @@ -0,0 +1,34 @@ +package commandline.commands; + +import java.io.File; +import java.util.List; +import java.util.Objects; + + +public class Ls implements Command { + @Override + public String run(List args, String before) { + StringBuilder resString = new StringBuilder(); + if (args.isEmpty()) { + File curDir = new File("."); + for (File file : Objects.requireNonNull(curDir.listFiles())) { + String name = file.getName(); + if (!name.startsWith(".")) { + resString.append(name).append(" "); + } + } + } else { + File curDir = new File(args.get(0)); + if (curDir.isDirectory()) { + for (File file : Objects.requireNonNull(curDir.listFiles())) { + resString.append(file.getName()).append(" "); + } + } + if (curDir.isFile()) { + resString.append(curDir.getName()).append(" "); + } + } + return resString.toString().trim(); + } + +} diff --git a/src/main/java/commandline/utils/CommandBuilder.java b/src/main/java/commandline/utils/CommandBuilder.java index 1b0f0a6..6e591b9 100644 --- a/src/main/java/commandline/utils/CommandBuilder.java +++ b/src/main/java/commandline/utils/CommandBuilder.java @@ -7,20 +7,24 @@ * else returns null */ public class CommandBuilder { - private CommandBuilder() { - } + private CommandBuilder() { + } - public static Command getCommand(String commandName) { - if (commandName.equals("pwd")) { - return new Pwd(); - } else if (commandName.equalsIgnoreCase("cat")) { - return new Cat(); - } else if (commandName.equalsIgnoreCase("echo")) { - return new Echo(); - } else if (commandName.equalsIgnoreCase("wc")) { - return new Wc(); - } else { - return null; - } + public static Command getCommand(String commandName) { + if (commandName.equals("pwd")) { + return new Pwd(); + } else if (commandName.equalsIgnoreCase("cat")) { + return new Cat(); + } else if (commandName.equalsIgnoreCase("echo")) { + return new Echo(); + } else if (commandName.equalsIgnoreCase("wc")) { + return new Wc(); + } else if (commandName.equalsIgnoreCase("ls")) { + return new Ls(); + } else if (commandName.equalsIgnoreCase("cd")) { + return new Cd(); + } else { + return null; } + } } diff --git a/src/test/java/commandline/CommandLineTest.java b/src/test/java/commandline/CommandLineTest.java index 64c5dee..2996184 100644 --- a/src/test/java/commandline/CommandLineTest.java +++ b/src/test/java/commandline/CommandLineTest.java @@ -1,11 +1,15 @@ package commandline; +import commandline.commands.Cd; +import commandline.commands.Command; +import commandline.commands.Ls; import commandline.utils.ShellCommand; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.ArrayList; // Testing class CommandLineTest { @@ -60,11 +64,11 @@ void testEcho() throws IOException, InterruptedException { } /*Test variables - * a=p b=wd $a$b == command pwd - * a=ec b=ho $a$b 5 == echo 5 == 5 - * a=ec b=ho echo $a$b == echo echo == echo - * a=ec b=ho echo $a $b == echo ec ho == ec ho - * */ + * a=p b=wd $a$b == command pwd + * a=ec b=ho $a$b 5 == echo 5 == 5 + * a=ec b=ho echo $a$b == echo echo == echo + * a=ec b=ho echo $a $b == echo ec ho == ec ho + * */ @Test void testVars() throws IOException, InterruptedException { String input = "a=p \n b=wd \n $a$b \n exit"; @@ -132,4 +136,32 @@ void testOtherCommands() { input = "git lolol \n exit"; Assertions.assertEquals("Wrong command: git lolol\n", out(input)); } + + @Test + void testLsCommands() { + Command ls = new Ls(); + ArrayList lst = new ArrayList<>(); + String expected = "build build.gradle.kts gradle gradlew gradlew.bat README.md settings.gradle src testFiles"; + Assertions.assertEquals(expected, ls.run(lst, "")); + lst.add("src"); + expected = "main test"; + Assertions.assertEquals(expected, ls.run(lst, "")); + lst.clear(); + expected = "README.MD"; + lst.add(expected); + Assertions.assertEquals(expected, ls.run(lst, "")); + } + + @Test + void testCdCommands() { + Command cd = new Cd(); + ArrayList lst = new ArrayList<>(); + String res = System.getProperty("user.dir"); + cd.run(lst, ""); + Assertions.assertEquals(System.getProperty("user.home"), System.getProperty("user.dir")); + lst.add("main"); + cd.run(lst, ""); + Assertions.assertEquals(System.getProperty("user.dir"), res + "\\" + "main"); + } + } From 56fe60981ece191dcb75bc94c481ab3fb608bdd0 Mon Sep 17 00:00:00 2001 From: slampy97 Date: Sun, 28 Mar 2021 06:42:20 +0300 Subject: [PATCH 2/3] add comments --- src/main/java/commandline/commands/Cd.java | 5 +++++ src/main/java/commandline/commands/Ls.java | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/commandline/commands/Cd.java b/src/main/java/commandline/commands/Cd.java index 79a099b..592bee2 100644 --- a/src/main/java/commandline/commands/Cd.java +++ b/src/main/java/commandline/commands/Cd.java @@ -3,6 +3,11 @@ import java.io.File; import java.util.List; +/* Function cd + * if it has 0 arguments, then it change current directory to + * home directory + * if it has one argument, then it change current directory to new one from arg + */ public class Cd implements Command { @Override public String run(List args, String before) { diff --git a/src/main/java/commandline/commands/Ls.java b/src/main/java/commandline/commands/Ls.java index 2d49374..f7edd88 100644 --- a/src/main/java/commandline/commands/Ls.java +++ b/src/main/java/commandline/commands/Ls.java @@ -4,7 +4,12 @@ import java.util.List; import java.util.Objects; - +/* Function ls + * if it has 0 arguments, then it print all filenames and fir name + * in current directory, ignoring starting with ./ or ../ + * if it has one argument, if it file, its just print filename + * if it directory, just print content from this directory + */ public class Ls implements Command { @Override public String run(List args, String before) { From da95cf9109294bbac1d405d31fc0e0c8bdf48e54 Mon Sep 17 00:00:00 2001 From: slampy97 Date: Sun, 11 Apr 2021 03:36:21 +0300 Subject: [PATCH 3/3] fix --- .idea/.gitignore | 3 + .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/compiler.xml | 6 ++ .idea/gradle.xml | 17 +++++ .idea/jarRepositories.xml | 20 ++++++ .idea/libraries-with-intellij-classes.xml | 65 +++++++++++++++++++ .idea/misc.xml | 4 ++ .idea/vcs.xml | 6 ++ src/main/java/commandline/commands/Cd.java | 13 +++- src/main/java/commandline/commands/Ls.java | 9 ++- .../java/commandline/CommandLineTest.java | 27 +++++++- 11 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries-with-intellij-classes.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..69921a0 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..fdc392f --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries-with-intellij-classes.xml b/.idea/libraries-with-intellij-classes.xml new file mode 100644 index 0000000..9fa3156 --- /dev/null +++ b/.idea/libraries-with-intellij-classes.xml @@ -0,0 +1,65 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6ed36dd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/commandline/commands/Cd.java b/src/main/java/commandline/commands/Cd.java index 592bee2..5034e91 100644 --- a/src/main/java/commandline/commands/Cd.java +++ b/src/main/java/commandline/commands/Cd.java @@ -1,6 +1,10 @@ package commandline.commands; +import java.awt.geom.Path2D; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; /* Function cd @@ -14,10 +18,13 @@ public String run(List args, String before) { if (args.isEmpty()) { System.setProperty("user.dir", System.getProperty("user.home")); } else { - File file = new File(args.get(0)); - System.setProperty("user.dir", file.getAbsolutePath()); + var path = Paths.get(System.getProperty("user.dir"), args.get(1)); + if (!Files.isDirectory(path)) { + return "no such directory"; + } + System.setProperty("user.dir", path.toAbsolutePath().toString()); } - return null; + return ""; } } diff --git a/src/main/java/commandline/commands/Ls.java b/src/main/java/commandline/commands/Ls.java index f7edd88..2d078d3 100644 --- a/src/main/java/commandline/commands/Ls.java +++ b/src/main/java/commandline/commands/Ls.java @@ -1,6 +1,7 @@ package commandline.commands; import java.io.File; +import java.nio.file.Paths; import java.util.List; import java.util.Objects; @@ -15,15 +16,13 @@ public class Ls implements Command { public String run(List args, String before) { StringBuilder resString = new StringBuilder(); if (args.isEmpty()) { - File curDir = new File("."); + File curDir = Paths.get(System.getProperty("user.dir")).toFile(); for (File file : Objects.requireNonNull(curDir.listFiles())) { String name = file.getName(); - if (!name.startsWith(".")) { - resString.append(name).append(" "); - } + resString.append(name).append(" "); } } else { - File curDir = new File(args.get(0)); + File curDir = Paths.get(System.getProperty("user.dir"), args.get(1)).toFile(); if (curDir.isDirectory()) { for (File file : Objects.requireNonNull(curDir.listFiles())) { resString.append(file.getName()).append(" "); diff --git a/src/test/java/commandline/CommandLineTest.java b/src/test/java/commandline/CommandLineTest.java index 2996184..69137db 100644 --- a/src/test/java/commandline/CommandLineTest.java +++ b/src/test/java/commandline/CommandLineTest.java @@ -141,27 +141,48 @@ void testOtherCommands() { void testLsCommands() { Command ls = new Ls(); ArrayList lst = new ArrayList<>(); - String expected = "build build.gradle.kts gradle gradlew gradlew.bat README.md settings.gradle src testFiles"; + String expected = ".git .github .gradle .idea build build.gradle.kts gradle gradlew gradlew.bat README.md settings.gradle src testFiles"; Assertions.assertEquals(expected, ls.run(lst, "")); + lst.add(""); lst.add("src"); expected = "main test"; Assertions.assertEquals(expected, ls.run(lst, "")); lst.clear(); expected = "README.MD"; + lst.add(""); lst.add(expected); Assertions.assertEquals(expected, ls.run(lst, "")); } @Test - void testCdCommands() { + void testCdCommands() throws IOException, InterruptedException { Command cd = new Cd(); ArrayList lst = new ArrayList<>(); String res = System.getProperty("user.dir"); cd.run(lst, ""); Assertions.assertEquals(System.getProperty("user.home"), System.getProperty("user.dir")); + lst.add(""); lst.add("main"); cd.run(lst, ""); - Assertions.assertEquals(System.getProperty("user.dir"), res + "\\" + "main"); + Assertions.assertEquals("D:\\programming\\sd_fork\\CLI\\main", res + "\\" + "main"); + String input = "ls \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + + input = "cd gradle | ls \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + + input = "cd build | ls classes \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + + input = "cd build | cd classes | ls \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + + input = "cd build | cd classes | ls java \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + + input = "cd build | cd classes | cd java | ls \n exit"; + Assertions.assertEquals(ShellCommand.executeCommand(input), out(input)); + } }