-
Notifications
You must be signed in to change notification settings - Fork 1
ls & cd #4
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: grep-command
Are you sure you want to change the base?
ls & cd #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package ru.tretikoff.commands | ||
|
|
||
| import ru.tretikoff.streams.Stream | ||
| import java.io.File | ||
| import java.util.logging.Logger | ||
|
|
||
| class CdCommand( | ||
| ins: Stream, | ||
| out: Stream, | ||
| err: Stream, | ||
| args: MutableList<String>, | ||
| ) : | ||
| Command(ins, out, err, args) { | ||
| private val logger = Logger.getLogger(CatCommand::class.java.name) | ||
| override fun execute(): Int { | ||
| logger.finest("Running cd with arguments $args") | ||
| if (args.isEmpty()) { | ||
| val path = System.getProperty("user.home") | ||
| return cdPath(path) | ||
| } else if (args.count() == 1) { | ||
| return cdPath(args.get(0)) | ||
| } else { | ||
| outputStream.writeLine("usage: cd [path]") | ||
| return 1 | ||
| } | ||
| } | ||
|
|
||
| private fun cdPath(path: String): Int { | ||
| val file = File(path).getAbsoluteFile() | ||
| if (!file.exists()) { | ||
| errorStream.writeLine("no such directory: \"$path\"") | ||
| return 2 | ||
| } else if (!file.isDirectory()) { | ||
| errorStream.writeLine("not a directory: \"$path\"") | ||
| return 3 | ||
| } | ||
| System.setProperty("user.dir", file.getAbsolutePath()) | ||
|
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.
|
||
| return 0 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ru.tretikoff.commands | ||
|
|
||
| import ru.tretikoff.streams.Stream | ||
| import java.io.File | ||
| import java.util.logging.Logger | ||
|
|
||
| class LsCommand( | ||
| ins: Stream, | ||
| out: Stream, | ||
| err: Stream, | ||
| args: MutableList<String>, | ||
| ) : | ||
| Command(ins, out, err, args) { | ||
| private val logger = Logger.getLogger(CatCommand::class.java.name) | ||
| override fun execute(): Int { | ||
| logger.finest("Running ls with arguments $args") | ||
| if (args.isEmpty()) { | ||
| val path = System.getProperty("user.dir") | ||
| return lsPath(path) | ||
| } else if (args.count() == 1) { | ||
| return lsPath(args.get(0)) | ||
|
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. `args[0]`` |
||
| } else { | ||
| outputStream.writeLine("usage: ls [path]") | ||
| return 1 | ||
| } | ||
| } | ||
|
|
||
| private fun lsPath(path: String): Int { | ||
| var parent = "" | ||
| if (!path.startsWith("/")) { | ||
| parent = System.getProperty("user.dir") | ||
| } | ||
| val file = File(parent, path).getAbsoluteFile() | ||
| val files = file.list() | ||
|
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 (!file.exists()) { | ||
| errorStream.writeLine("no such file or directory: \"$path\"") | ||
| return 2 | ||
| } | ||
| if (files == null) { | ||
| outputStream.writeLine(path) | ||
| } else { | ||
| files.forEach { | ||
| if (!it.startsWith(".")) { | ||
| outputStream.writeLine(it) | ||
| } | ||
| } | ||
| } | ||
| return 0 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.tretikoff | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class CdTest : BashTest() { | ||
| @Test | ||
| fun cdHome() { | ||
| stdin.writeLine("cd") | ||
| stdin.writeLine("pwd") | ||
| shell.run() | ||
| assertEquals(System.getProperty("user.home"), stdout.read()) | ||
| } | ||
|
|
||
| @Test | ||
| fun cdDir() { | ||
| stdin.writeLine("cd src") | ||
| stdin.writeLine("ls") | ||
| shell.run() | ||
| assertEquals("main", stdout.read()) | ||
| assertEquals("test", stdout.read()) | ||
| } | ||
|
|
||
| @Test | ||
| fun cdErr() { | ||
| stdin.writeLine("cd abaaa") | ||
| shell.run() | ||
| assertEquals("no such directory: \"abaaa\"", stderr.read()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package ru.tretikoff | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class LsTest : BashTest() { | ||
| @Test | ||
| fun lsDir() { | ||
| stdin.writeLine("ls src") | ||
| shell.run() | ||
| assertEquals("main", stdout.read()) | ||
| assertEquals("test", stdout.read()) | ||
| } | ||
|
|
||
| @Test | ||
| fun lsFile() { | ||
| stdin.writeLine("ls gradlew.bat") | ||
| shell.run() | ||
| assertEquals("gradlew.bat", stdout.read()) | ||
| } | ||
|
|
||
| @Test | ||
| fun lsErr() { | ||
| stdin.writeLine("ls abaaa") | ||
| shell.run() | ||
| assertEquals("no such file or directory: \"abaaa\"", stderr.read()) | ||
| } | ||
| } |
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.
Вообще кажется плохой идеей так менять рабочую директорию, т.к. потом элементарно сложнее тестировать. Если это переменная окружения, то окружение можно замокать, а
System.getPropertyуже вряд ли.Сейчас не видно проблему, т.к. тесты не юнит, а все-таки интеграционные, но если бы они были юнит-тестами, то быстро бы всплыло, что от запуска одного теста к запуску другого приходилось бы танцевать с бубном, чтобы тесты правильно работали