-
Notifications
You must be signed in to change notification settings - Fork 78
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
[Reopen] Read args from file, merge args and pass to the cmd. #308
base: master
Are you sure you want to change the base?
Conversation
Also could i please get provide some guidance in fixing CI/CD error. I am new to this ecosystem and unclear on what exactly the errors are. |
This error is related to Scala JS, to use of a library scala-collection-compat. Probably version numbers for the Scala compiler(s) need updating to be consistent with each other. I would look in the build file. |
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.
Looks good! Please just add a test to ensure the behavior is working correctly, and th en we can merge!
As of 19th April zio-nio doesnt support scalaJS so using java to read file and zio core for resource management
Update: 22 Apr 2024: Notes:
Update 20 Apr 2024:
@jdegoes First, thank you for your review. I have commited fix for build errors in JVM and Native. However, errors are now only in scalaJS. I believe it is JS doesnt support Also, I'm working on writing test. should be done soon. Thank you again :) |
@jdegoes |
@vivasvan1 Hi! The tests do not cover the case of a real If you could add a test suite similar to this testing the whole process of running the
|
@pablf Hi there, I have added the test case you asked for. Where the functionality is being tested on the full wc app. I am not sure why your local is failing. Could you provide more details. Thanks, |
} | ||
) | ||
|
||
def createLineCountTestFile( |
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.
This should return
ZIO[Scope, IOException, Unit]
and clean itself up
|
||
def createLineCountTestFile( | ||
cwd: Path, | ||
file_name: String = "sample_file", |
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.
fileName
content_home: String = "home=true", | ||
content_cwd: String = "dir=true\nhome=false" |
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.
camel
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.
Also, is it possible to remove the default arguments?
I find reading through the tests very hard to follow, and easily determine what output I would expect.
Also, I imagine that there should be multiple tests asserting expected behavior for different permutations of hierarchy overrides.
command: String = "testApp", | ||
content_home: String = "home=true", | ||
content_cwd: String = "dir=true\nhome=false" | ||
): IO[IOException, Unit] = |
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.
same thing as above, writing to file should return zio Scope
, and clean itself up, doing so for a single file, and be called twice
val cwd = java.lang.System.getProperty("user.dir") | ||
val homeDirOpt = java.lang.System.getProperty("user.home") |
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.
See: zio.System
lines <- ZIO | ||
.foreach(filePaths) { filePath => | ||
ZIO.acquireReleaseWith( | ||
ZIO.attempt( | ||
Source.fromFile(Paths.get(filePath, "." + topLevelCommand).toFile) | ||
) | ||
)(source => ZIO.attempt(source.close()).orDie) { source => | ||
ZIO.attempt(source.getLines().toList.filter(_.trim.nonEmpty)) | ||
} | ||
} | ||
.map(_.flatten) |
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.
zio.ZIO.readFile
// Create Sample config files | ||
cwd <- ZIO.succeed(Paths.get(java.lang.System.getProperty("user.dir"))) | ||
homeDir <- ZIO.succeed(Paths.get(java.lang.System.getProperty("user.home"))) | ||
command <- ZIO.succeed("testApp") |
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.
command = "testApp"
// Create Sample config files | ||
cwd <- ZIO.succeed(Paths.get(java.lang.System.getProperty("user.dir"))) | ||
homeDir <- ZIO.succeed(Paths.get(java.lang.System.getProperty("user.home"))) | ||
command <- ZIO.succeed("wc") |
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.
command = "wc"
_ <- cliApp.run(args = List("sample_file")).either | ||
output <- TestConsole.output | ||
|
||
correctOutput <- ZIO.succeed(" 3 4 20 sample_file\n") |
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.
=
.flatMap { configArgs => | ||
ZIO.succeed(configArgs ++ args) | ||
} |
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.
.map
import java.nio.file.{Files, Path, Paths} | ||
import scala.io.Source | ||
|
||
object ConfigFileArgsPlatformSpecific extends ConfigFilePlatformSpecific { |
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.
This seems to be copy-pasted, if so, move into ./zio-cli/jvm-native
content: String = "asdf\nqweqwer\njdsafn" | ||
): IO[IOException, Unit] = | ||
ZIO.attempt { | ||
Files.write(Paths.get(cwd.toString(), s"$file_name"), java.util.Arrays.asList(content)); |
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.
content.getBytes
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.
s"$file_name"
= fileName
def parentPaths(path: String): List[String] = { | ||
val parts = path.split(java.io.File.separatorChar).filterNot(_.isEmpty) | ||
(0 to parts.length) | ||
.map(i => s"${java.io.File.separatorChar}${parts.take(i).mkString(java.io.File.separator)}") | ||
.toList | ||
} |
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.
Does this work for Windows
/Linux
/Mac
?
I have a sneaking suspicion it might not work correctly for /home/kalin/dev/a/b/c/.cmd
Might be safer to implement via Path#getParent
?
trait ConfigFilePlatformSpecific { | ||
def findPathsOfCliConfigFiles(topLevelCommand: String): Task[List[String]] | ||
def loadOptionsFromConfigFiles(topLevelCommand: String): ZIO[Any, IOException, List[String]] | ||
} |
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.
Personal opinion:
I think it would be nicer if this passed via something ~like
trait ConfigFileArgs {
// ...
}
object ConfigFileArgs {
val default: ConfigFileArgs // platform specific
}
sealed trait CliApp[-R, +E, +A] { self =>
def runWithFileArgs(args: List[String]): ZIO[R & ConfigFileArgs, CliError[E], Option[A]]
final def run(args: List[String]): ZIO[R, CliError[E], Option[A]] =
runWithFileArgs(args).provideSomeLayer(ZLayer.succeed(ConfigFileArgs.default))
}
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.
The reason I suggest this is because it would allow for more separation of concerns, and make much more testable.
You could independently test a Live
impl, verify that is doing what it should, and provide a Mock
layer to the actual cliApp.run
(or in this example cliApp.runWithFileArgs
), so that you arent doing all this file standup/teardown in order to test the internals of the parsing is working correctly
Again, very much personal opinion 😅
@Kalin-Rudnicki I will look into this asap. This seems really helpful. |
@vivasvan1 If I remember correctly, the tests contained in the code of my last comment should all pass if the implementation of the feature were correct, so if you copy that code into If you have any doubt, ask me please! Or maybe you have already tried adding the |
/claim #191
closes #191
Hi @jdegoes,
Hi have attempted to resolve your review comments on the previous PR
Please let me know if more changes are required.
Working Screenshots:
References:
P.S. : I dont have permission to reopen the previous PR so created a fresh one.