Skip to content

Commit

Permalink
Merge pull request #1208 from informalsystems/shon/fix-config-tilde-e…
Browse files Browse the repository at this point in the history
…xpansion

Fix expansion of ~ in configured paths
  • Loading branch information
Shon Feder authored Jan 13, 2022
2 parents b5634d6 + 1f54624 commit 8b02d1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
3 changes: 3 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
* Some bug fix, see #124
DO NOT LEAVE A BLANK LINE BELOW THIS PREAMBLE -->
### Bug fixes

* Fix expansion of `~` in configured paths, see #1208
17 changes: 17 additions & 0 deletions test/tla/cli-integration-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,23 @@ $ test -d ./configured-run-dir
$ rm -rf ./configured-run-dir ./cli-config.cfg
```
### configuration management: tilde is expanded in configured paths
We set `user.home` to the current working directly, so we can test tilde
expansion in the path without writing outside of the test directory.
NOTE: We need to set the home to a relative path to the cwd in order to
ensure the tests also works in the docker container.
```sh
$ echo "run-dir: ~/run-dir" > .apalache.cfg
$ JVM_ARGS="-Duser.home=." apalache-mc check --length=0 Counter.tla | sed 's/[IEW]@.*//'
...
EXITCODE: OK
$ test -d ./run-dir
$ rm -rf ./run-dir ./.apalache.cfg
```
## server mode
### server mode: subcommand is not yet implemented
Expand Down
38 changes: 11 additions & 27 deletions tla-io/src/main/scala/at/forsyte/apalache/io/ConfigManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,23 @@ import pureconfig._
import pureconfig.generic.auto._
import java.io.{File}
import java.nio.file.{Path, Files, Paths}
import com.typesafe.config.ConfigValueFactory
import com.typesafe.config.ConfigObject

object Converters {
// Provides implicit conversions used when deserializing into configurable values.
private object Converters {
import pureconfig.ConvertHelpers._

private def expandedFilePath(s: String): Path = {
Paths.get(if (s.startsWith("~")) s.replaceFirst("~", System.getProperty("user.home")) else s)
}

// Value class to allow adding a new implicit for the File
case class ExpandedFile(val file: File) extends AnyVal

object ExpandedFile {
def apply(f: File): ExpandedFile = {
new ExpandedFile(expandedFilePath(f.toString()).toFile())
}
}

// Value class to allow adding a new implicit for Path
case class ExpandedPath(val path: Path) extends AnyVal

object ExpandedPath {
def apply(f: Path): ExpandedPath = {
new ExpandedPath(expandedFilePath(f.toString()))
}
}

// Briniging these implicits in scope lets us override the existing
// file deserialization behavior, so we get path expansion in all configured
// paths
implicit def expandedFileConfigReader: ConfigReader[ExpandedFile] =
ConfigReader[File].map(ExpandedFile.apply)

implicit def expandedPathConfigReader: ConfigReader[ExpandedPath] =
ConfigReader[Path].map(ExpandedPath.apply)
// Briniging these implicits in scope lets us override the existing File and
// Path deserialization behavior, so we get path expansion in all configured
// paths.
// See https://pureconfig.github.io/docs/overriding-behavior-for-types.html
implicit val overridePathReader = ConfigReader.fromString[Path](catchReadError(expandedFilePath))
implicit val overrideFileReader = ConfigReader.fromString[File](catchReadError(expandedFilePath(_).toFile()))
}

/** The configuration values that can be overriden based on CLI arguments */
Expand Down

0 comments on commit 8b02d1d

Please sign in to comment.