Repository: apple/pkl
File: pkl-cli/src/test/kotlin/org/pkl/cli/CliEvaluatorTest.kt (Kotlin)

Code before:
          sourceModules = listOf(URI("http://not.a.valid.host/bar.pkl")),
          allowedModules = SecurityManagers.defaultAllowedModules + Pattern.compile("http:"),
          projectDir = projectDir,
          settings = homeDir.resolve("settings.pkl").toUri()
        ),
      )
    stubFor(get(anyUrl()).willReturn(ok("result = 1")))
    val output = evalToConsole(options)
    assertThat(output).isEqualTo("result = 1\n")
  }

  private fun evalModuleThatImportsPackage(certsFile: Path?, testPort: Int = -1) {
    val moduleUri =
      writePklFile(
        "test.pkl",
        """
      import "package://localhost:0/birds@0.5.0#/catalog/Swallow.pkl"
      
      res = Swallow
    """
      )

    val options =
      CliEvaluatorOptions(
        CliBaseOptions(
          sourceModules = listOf(moduleUri),
          caCertificates = buildList { if (certsFile != null) add(certsFile) },
          workingDir = tempDir,
          noCache = true,
          testPort = testPort
        ),
      )
    CliEvaluator(options).run()
  }

  private fun writePklFile(fileName: String, contents: String = defaultContents): URI {
    tempDir.resolve(fileName).createParentDirectories()
    return tempDir.resolve(fileName).writeString(contents).toUri()
  }

  private fun evalToFiles(options: CliEvaluatorOptions): List<Path> {
    val evaluator =
      CliEvaluator(
        options.copy(
          outputPath = options.outputPath ?: "%{moduleDir}/%{moduleName}.%{outputFormat}"
        )
      )

    evaluator.run()
    return evaluator.fileOutputPaths!!.values.toList()
  }

Review comment (bug):
This will fail on Windows thanks to different filepath separators.

Better to do:

```
val tempDirUri = tempDir.toUri()
```

And use that in the code snippet instead, e.g.

```
read("$tempDirUri/日本語.pkl")
```

Code after:
          sourceModules = listOf(URI("http://not.a.valid.host/bar.pkl")),
          allowedModules = SecurityManagers.defaultAllowedModules + Pattern.compile("http:"),
          projectDir = projectDir,
          settings = homeDir.resolve("settings.pkl").toUri()
        ),
      )
    stubFor(get(anyUrl()).willReturn(ok("result = 1")))
    val output = evalToConsole(options)
    assertThat(output).isEqualTo("result = 1\n")
  }

  @Test
  fun `eval file with non-ASCII name`() {
    val tempDirUri = tempDir.toUri()
    val dir = tempDir.resolve("🤬").createDirectory()
    val file =
      writePklFile(
        dir.resolve("日本語.pkl").toString(),
        """
      日本語 = "Japanese language"
      readDir = read(".").text
      readDirFile = read("$tempDirUri🤬").text
      readOne = read("日本語.pkl").text.split("\n").first
      readOneFile = read("$tempDirUri🤬/日本語.pkl").text.split("\n").first
      readGlob = read*("./日*.pkl").keys
      readGlobFile = read*("$tempDirUri**/*.pkl").keys.map((it) -> it.replaceAll("$tempDirUri".replaceAll("///", "/"), ""))
      importOne = import("日本語.pkl").readOne
      importOneFile = import("$tempDirUri🤬/日本語.pkl").日本語
      importGlob = import*("./日*.pkl").keys
      importGlobFile = import*("$tempDirUri**/*.pkl").keys.map((it) -> it.replaceAll("$tempDirUri".replaceAll("///", "/"), ""))
    """
          .trimIndent()
      )
    val output =
      evalToConsole(
        CliEvaluatorOptions(
          CliBaseOptions(sourceModules = listOf(file)),
        )
      )

    val tripleQuote = "\"\"\""
    assertThat(output)
      .isEqualTo(
        """
        日本語 = "Japanese language"
        readDir = $tripleQuote
          日本語.pkl
          
          $tripleQuote
        readDirFile = $tripleQuote
          日本語.pkl