-
Notifications
You must be signed in to change notification settings - Fork 1
Деркунский Виктор. HW 3 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: HW1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 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 | ||
| * 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<String> args, String before) { | ||
| if (args.isEmpty()) { | ||
| System.setProperty("user.dir", System.getProperty("user.home")); | ||
| } else { | ||
| var path = Paths.get(System.getProperty("user.dir"), args.get(1)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Все равно лучше было сделать внутреннюю переменную для этого или прям переменной окружения |
||
| if (!Files.isDirectory(path)) { | ||
| return "no such directory"; | ||
| } | ||
| System.setProperty("user.dir", path.toAbsolutePath().toString()); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package commandline.commands; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Paths; | ||
| 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<String> args, String before) { | ||
| StringBuilder resString = new StringBuilder(); | ||
| if (args.isEmpty()) { | ||
| File curDir = Paths.get(System.getProperty("user.dir")).toFile(); | ||
| for (File file : Objects.requireNonNull(curDir.listFiles())) { | ||
| String name = file.getName(); | ||
| resString.append(name).append(" "); | ||
| } | ||
| } else { | ||
| 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(" "); | ||
| } | ||
| } | ||
| if (curDir.isFile()) { | ||
| resString.append(curDir.getName()).append(" "); | ||
| } | ||
| } | ||
| return resString.toString().trim(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А что с форматированием стало? Почему 4 пробела вдруг на 2 заменились?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в ксц на java другие требования, забываю в editor менять |
||
| } 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; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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,53 @@ void testOtherCommands() { | |
| input = "git lolol \n exit"; | ||
| Assertions.assertEquals("Wrong command: git lolol\n", out(input)); | ||
| } | ||
|
|
||
| @Test | ||
| void testLsCommands() { | ||
| Command ls = new Ls(); | ||
| ArrayList<String> lst = new ArrayList<>(); | ||
| String expected = ".git .github .gradle .idea build build.gradle.kts gradle gradlew gradlew.bat README.md settings.gradle src testFiles"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вообще такие тесты плохие, т.к. содержимое корневой директории очевидно еще будет меняться |
||
| 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() throws IOException, InterruptedException { | ||
| Command cd = new Cd(); | ||
| ArrayList<String> 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("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)); | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень зря так меняете текущую директорию, как минимум сложно написать тесты на такой
cd