kunwp1 commented on code in PR #9479:
URL: https://github.com/apache/texera/pull/9479#discussion_r4592937491


##########
common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/codegen/HuggingFaceCodegenFixtures.scala:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.amber.operator.huggingFace.codegen
+
+import org.scalatest.matchers.should.Matchers
+
+private[codegen] object HuggingFaceCodegenFixtures extends Matchers {
+  val textContext: CodegenContext = CodegenContext(
+    hfApiToken = "token",
+    modelId = "Qwen/Qwen2.5-72B-Instruct",
+    promptColumn = "prompt",
+    resultColumn = "hf_response",
+    task = "text-generation",
+    systemPrompt = "You are a helpful assistant.",
+    safeMaxTokens = 256,
+    safeTemp = 0.7
+  )
+
+  private def variedContext(suffix: String, maxTokens: Int, temperature: 
Double): CodegenContext =
+    CodegenContext(
+      hfApiToken = s"token-$suffix",
+      modelId = s"model-$suffix",
+      promptColumn = s"col-$suffix",
+      resultColumn = s"result-$suffix",
+      task = s"task-$suffix",
+      systemPrompt = s"system-$suffix",
+      safeMaxTokens = maxTokens,
+      safeTemp = temperature,
+      imageInput = s"image-$suffix",
+      inputImageColumn = s"image-col-$suffix",
+      audioInput = s"audio-$suffix",
+      inputAudioColumn = s"audio-col-$suffix",
+      contextColumn = s"ctx-$suffix",
+      candidateLabels = s"labels-$suffix",
+      sentencesColumn = s"sent-$suffix"
+    )
+
+  def assertNoRawContextValues(codegen: TaskCodegen): Unit = {
+    val ctx = variedContext("MARKER_zXyq42", 256, 0.7)
+    val payload = codegen.payloadPython(ctx)
+    val parse = codegen.parsePython(ctx)
+    // Probe every string field in the fixture, including fields specific to 
other task families.
+    ctx.productIterator.collect { case marker: String => marker }.foreach { 
marker =>
+      payload should not include marker
+      parse should not include marker
+    }
+  }
+
+  def assertStaticSnippets(
+      codegen: TaskCodegen,
+      originalTask: String,
+      otherTask: Option[String] = None

Review Comment:
   `otherTask` has no caller yet. `ImageTaskCodegenSpec`, the one spec that 
needs it, still has both checks written out:
   
   ```scala
   assertNoRawContextValues(ImageTaskCodegen)
   assertStaticSnippets(ImageTaskCodegen, "image-classification", 
Some("zero-shot-image-classification"))
   ```
   
   `HuggingFaceParseBehaviorSpec` likewise still keeps its own interpreter 
lookup, probe copy and file-redirect runner. It could use `configuredPython()`, 
`copyScriptResource`, `withProbe(exe, probe, "hf_parse_probe", 60, ...)` and 
`textContext.copy(task = task)`, the same way 
`HuggingFaceBinaryValueBehaviorSpec` now does.



##########
common/workflow-operator/src/test/scala/org/apache/texera/amber/util/PythonTestSupport.scala:
##########
@@ -64,4 +69,117 @@ private[amber] object PythonTestSupport {
         false
       }
     }
+
+  private def readDefault(stream: InputStream): String =
+    Source.fromInputStream(stream).mkString
+
+  def readUtf8(stream: InputStream): String =
+    Source.fromInputStream(stream, "UTF-8").mkString
+
+  def readUtf8WithReplacement(stream: InputStream): String =
+    new String(stream.readAllBytes(), UTF_8)
+
+  def runSource(
+      python: String,
+      prefix: String,
+      code: String,
+      timeoutSeconds: Int = 120,
+      environment: Map[String, String] = Map.empty,
+      readOutput: InputStream => String = readDefault
+  ): (Int, String) = {
+    val script = Files.createTempFile(prefix, ".py")
+    script.toFile.deleteOnExit()
+    Files.write(script, code.getBytes(UTF_8))
+    runScript(python, script, timeoutSeconds, environment = environment, 
readOutput = readOutput)
+  }
+
+  def runScript(
+      python: String,
+      script: Path,
+      timeoutSeconds: Int = 120,
+      directory: Option[Path] = None,
+      environment: Map[String, String] = Map.empty,
+      readOutput: InputStream => String = readDefault
+  ): (Int, String) = {
+    val builder = new ProcessBuilder(python, 
script.toString).redirectErrorStream(true)
+    directory.foreach(path => builder.directory(path.toFile))
+    environment.foreach { case (key, value) => builder.environment().put(key, 
value) }
+    val process = builder.start()
+    val output = readOutput(process.getInputStream)
+    process.waitFor(timeoutSeconds, TimeUnit.SECONDS)
+    (process.exitValue(), output)
+  }

Review Comment:
   Could `runScript` and `withDriver` send the output to a file the way 
`withProbe` does? `runScript` reads to EOF before `waitFor`, so its timeout 
(300 s in Histogram2D) can't stop a hung script. `withDriver` waits before 
draining the pipe, so a driver that prints more than the pipe buffer stalls 
until the 120 s timeout. 
   
   One private runner like that can serve both. It also lets the three readers 
become one UTF-8 read, and `bestEffortCleanup` can go, since only TreePlot and 
the scorer pass `false`. The comment in `HuggingFaceParseBehaviorSpec` 
describes the same hazard.



##########
common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/GeneratedCodeAssertions.scala:
##########
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.amber.operator
+
+import java.nio.charset.StandardCharsets
+import java.util.Base64
+
+private[operator] object GeneratedCodeAssertions {
+  def b64(s: String): String =
+    Base64.getEncoder.encodeToString(s.getBytes(StandardCharsets.UTF_8))
+
+  def carries(output: String, name: String): Boolean =
+    output.contains(name) || output.contains(b64(name))
+
+  def carriesEncoded(output: String, name: String): Boolean =
+    output.contains(b64(name))
+
+  def fieldPart(msg: String): String =
+    msg.toLowerCase.replace("cannot be empty", "")

Review Comment:
   The comments that described the deleted local copies are still in 12 specs, 
now above unrelated code. For example, `NestedTableOpDescSpec:39` sits above 
`config`, and `HeatMapOpDescSpec:37` above the `operatorInfo` test. Keeping one 
copy of each here and dropping the rest puts them next to what they describe.
   
   Separately, the four `HuggingFace*OpDescSpec`s still define the Base64-only 
`carries` that `EncodedGeneratedCodeAssertionChecks` now provides, and 
`VolcanoPlotOpDescSpec` still has its own `b64`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to