nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: bwplotka/mdox
Pull request #103 (2023-01-24 18:12:13)

mdox-exec: Also add newline when non-zero exit code
A new line (`\n`) is added at the end of `mdox-exec` output, but only when the exit code is 0 (not when using a non-zero `mdox-expect-exit-code`):

https://github.com/bwplotka/mdox/blob/53a5104b2ef519f3e35ebb0605c86121c503bc38/pkg/mdformatter/mdgen/mdgen.go#L92-L95

The new line should always be added (if not present).

Tests are updated to use `echo -n` (prints no `\n`).


Patch:
diff --git a/pkg/mdformatter/mdgen/mdgen.go b/pkg/mdformatter/mdgen/mdgen.go
index d00a8f8..d179108 100644
--- a/pkg/mdformatter/mdgen/mdgen.go
+++ b/pkg/mdformatter/mdgen/mdgen.go
@@ -83,10 +83,13 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
 		cmd.Stdout = &b
 		if err := cmd.Run(); err != nil {
 			expectedCode, _ := strconv.Atoi(infoStringAttr[infoStringKeyExitCode])
-			if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == expectedCode {
-				return b.Bytes(), nil
+			if exitErr, ok := err.(*exec.ExitError); ok {
+				if exitErr.ExitCode() != expectedCode {
+					return nil, errors.Wrapf(err, "run %v, expected exit code %v, got %v, out: %v", execCmd, expectedCode, exitErr.ExitCode(), b.String())
+				}
+			} else {
+				return nil, errors.Wrapf(err, "run %v, out: %v", execCmd, b.String())
 			}
-			return nil, errors.Wrapf(err, "run %v, out: %v", execCmd, b.String())
 		}
 		output := b.Bytes()
 		// Add newline to output if not present.


Test patch:
diff --git a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
index 5e61df8..726adbd 100755
--- a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
+++ b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
@@ -34,7 +34,7 @@ test output3
 ```bash mdox-exec="sed -n '1,3p' ./testdata/out3.sh"
 #!/usr/bin/env bash
 
-echo "test output3"
+echo -n "test output3"
 ```
 
 ```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig"
@@ -45,6 +45,6 @@ newline
 ```bash mdox-exec="cat ./testdata/out3.sh"
 #!/usr/bin/env bash
 
-echo "test output3"
+echo -n "test output3"
 exit 2
 ```
diff --git a/pkg/mdformatter/mdgen/testdata/out3.sh b/pkg/mdformatter/mdgen/testdata/out3.sh
index eafec28..953dfd0 100755
--- a/pkg/mdformatter/mdgen/testdata/out3.sh
+++ b/pkg/mdformatter/mdgen/testdata/out3.sh
@@ -1,4 +1,4 @@
 #!/usr/bin/env bash
 
-echo "test output3"
+echo -n "test output3"
 exit 2
\ No newline at end of file