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
Empty file modified gradlew
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/tretikoff/CommandFactory.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ru.tretikoff

import ru.tretikoff.commands.CatCommand
import ru.tretikoff.commands.CdCommand
import ru.tretikoff.commands.Command
import ru.tretikoff.commands.EchoCommand
import ru.tretikoff.commands.ExitCommand
import ru.tretikoff.commands.ExternalCommand
import ru.tretikoff.commands.GrepCommand
import ru.tretikoff.commands.LsCommand
import ru.tretikoff.commands.PwdCommand
import ru.tretikoff.commands.WcCommand
import ru.tretikoff.streams.Stream
Expand Down Expand Up @@ -33,6 +35,8 @@ class CommandFactory private constructor() {
"pwd" -> PwdCommand(ins, outs, errs, args)
"wc" -> WcCommand(ins, outs, errs, args)
"grep" -> GrepCommand(ins, outs, errs, args)
"ls" -> LsCommand(ins, outs, errs, args)
"cd" -> CdCommand(ins, outs, errs, args)
else -> ExternalCommand(ins, outs, errs, tokens.toMutableList(), tokens.first())
}
checkKeys(command, words)
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/ru/tretikoff/commands/CdCommand.kt
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")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вообще кажется плохой идеей так менять рабочую директорию, т.к. потом элементарно сложнее тестировать. Если это переменная окружения, то окружение можно замокать, а System.getProperty уже вряд ли.

Сейчас не видно проблему, т.к. тесты не юнит, а все-таки интеграционные, но если бы они были юнит-тестами, то быстро бы всплыло, что от запуска одного теста к запуску другого приходилось бы танцевать с бубном, чтобы тесты правильно работали

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())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

file.agbsolutePath, это же котлин

return 0
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/ru/tretikoff/commands/ExternalCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.tretikoff.commands
import ru.tretikoff.CommandNotFoundException
import ru.tretikoff.streams.Stream
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.util.logging.Logger
Expand All @@ -23,7 +24,7 @@ class ExternalCommand(

override fun execute(): Int {
try {
val p = Runtime.getRuntime().exec(args[0])
val p = Runtime.getRuntime().exec(args[0], null, File(System.getProperty("user.dir")))
p.waitFor()
val stdInput = BufferedReader(InputStreamReader(p.inputStream))
val stdError = BufferedReader(InputStreamReader(p.errorStream))
Expand Down
50 changes: 50 additions & 0 deletions src/main/kotlin/ru/tretikoff/commands/LsCommand.kt
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))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Чего-то не понял почему мы сначала пытаемся list взять, а только потом проверяем существует ли вообще файл

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
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/ru/tretikoff/streams/FileStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ open class FileStream(filename: String) : Stream() {

init {
try {
file = File(filename)
var parent = ""
if (!filename.startsWith("/")) {
parent = System.getProperty("user.dir")
}
file = File(parent, filename).getAbsoluteFile()
outputStream = FileOutputStream(file, true)
br = file.inputStream().bufferedReader()
} catch (e: FileNotFoundException) {
Expand Down
30 changes: 30 additions & 0 deletions src/test/kotlin/ru/tretikoff/CdTest.kt
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())
}
}
28 changes: 28 additions & 0 deletions src/test/kotlin/ru/tretikoff/LsTest.kt
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())
}
}