aglinxinyuan commented on code in PR #9479: URL: https://github.com/apache/texera/pull/9479#discussion_r4593153888
########## 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: Done in 035c196. One copy of each comment now sits on `carries` and `fieldPart` in `GeneratedCodeAssertions`, and the Quiver/Radar note about `.encode` wrapping is folded into the `EncodedGeneratedCodeAssertionChecks` comment. The stale copies are gone from all 14 specs (your 12 plus Quiver and Radar). The four `HuggingFace*OpDescSpec`s now mix in `EncodedGeneratedCodeAssertionChecks` instead of defining their own `carries`, and `VolcanoPlotOpDescSpec` uses the shared `b64`. ########## 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: Done in 035c196. `ImageTaskCodegenSpec` now uses `textContext`, `assertNoRawContextValues(ImageTaskCodegen)` and `assertStaticSnippets(ImageTaskCodegen, "image-classification", Some("zero-shot-image-classification"))`. `HuggingFaceParseBehaviorSpec` now uses `configuredPython()`, `copyScriptResource`, `withProbe(exe, probe, "hf_parse_probe", 60, ...)` and `textContext.copy(task = task)`. ########## 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: Done in 035c196. `runScript` and `withDriver` now go through one private `run` that redirects stdout and stderr to a temp file, waits up to the timeout, and fails with the timeout message (`runScript` names the script). The three readers are one UTF-8 read, and `run` sets `PYTHONIOENCODING=utf-8` so Python writes what we decode on every platform, which lets Regex and SubstringSearch stop passing it. `bestEffortCleanup` is gone and cleanup is always best-effort. The hazard comment moved from `HuggingFaceParseBehaviorSpec` onto `run`. -- 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]
