Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions .idea/libraries-with-intellij-classes.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/main/java/commandline/commands/Cd.java
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"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Очень зря так меняете текущую директорию, как минимум сложно написать тесты на такой cd

} else {
var path = Paths.get(System.getProperty("user.dir"), args.get(1));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 "";
}

}
38 changes: 38 additions & 0 deletions src/main/java/commandline/commands/Ls.java
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();
}

}
32 changes: 18 additions & 14 deletions src/main/java/commandline/utils/CommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что с форматированием стало? Почему 4 пробела вдруг на 2 заменились?

Copy link
Author

@slampy97 slampy97 Apr 11, 2021

Choose a reason for hiding this comment

The 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;
}
}
}
63 changes: 58 additions & 5 deletions src/test/java/commandline/CommandLineTest.java
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 {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Copy link
Collaborator

Choose a reason for hiding this comment

The 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));

}

}