Skip to content

Commit

Permalink
Add python runner (#11)
Browse files Browse the repository at this point in the history
* chore(feat): add python support

* chore: add docker build for python base image

* chore: fix run.sh missing if block
  • Loading branch information
nully0x authored Nov 27, 2024
1 parent ab878bd commit 29fa715
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/staging-build-base-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
tag: "ts-base",
path: "baseImages/typescript",
}
- { name: "python", tag: "py-base", path: "baseImages/python" }

steps:
- name: Checkout Code
Expand Down
1 change: 1 addition & 0 deletions baseImages/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM python:3.9-slim
11 changes: 11 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export const LANGUAGE_CONFIGS: Record<string, LanguageConfig> = {
),
runCommand: "/app/.hxckr/run.sh",
},
python: {
language: "python",
dockerfilePath: path.join(
__dirname,
"../../supportedLanguageDockerfiles/python/Dockerfile",
),
runCommand: "/app/.hxckr/run.sh",
},
// Add other languages as needed
};

Expand All @@ -38,6 +46,9 @@ export function detectLanguage(repoDir: string): string {
if (fs.existsSync(path.join(repoDir, "Cargo.toml"))) {
return "rust";
}
if (fs.existsSync(path.join(repoDir, "requirements.txt"))) {
return "python";
}
throw new Error("Unsupported language");

// going to add other language detection logic as needed
Expand Down
46 changes: 33 additions & 13 deletions src/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ export async function runTestProcess(request: TestRunRequest): Promise<void> {

// Create run.sh with modified commands
const runScript = `#!/bin/bash
set -e # Exit on any error
# Run the tests only (they will execute the code as part of the tests)
bun test ./app/stage${progress.progress_details.current_step}${TestRepoManager.getTestExtension(language)}
# Run the tests based on the language
if [ -f "requirements.txt" ]; then
# For Python projects
pytest ./app/stage${progress.progress_details.current_step}${TestRepoManager.getTestExtension(language)} -v
else
# For TypeScript projects
bun test ./app/stage${progress.progress_details.current_step}${TestRepoManager.getTestExtension(language)}
fi
`;

const runScriptPath = path.join(repoDir, ".hxckr", "run.sh");
Expand Down Expand Up @@ -131,19 +136,34 @@ export async function runTestProcess(request: TestRunRequest): Promise<void> {
}

function isTestSuccessful(output: string): boolean {
// Check if all tests passed
const failMatch = output.match(/(\d+)\s+fail/);
const failCount = failMatch ? parseInt(failMatch[1]) : 0;
return failCount === 0;
if (output.includes("pytest")) {
// For Python tests - check for failures and errors
const hasFailed =
output.includes(" FAILED ") ||
output.includes("= FAILURES =") ||
output.includes(" ERROR ") ||
output.includes("!!!!!!!!!!!!!!!!!!!! Interrupted:");
return !hasFailed;
} else {
// For TypeScript tests (unchanged)
const failMatch = output.match(/(\d+)\s+fail/);
const failCount = failMatch ? parseInt(failMatch[1]) : 0;
return failCount === 0;
}
}

function cleanTestOutput(output: string): string {
// Remove the duplicate output and clean up the format
const testRunMatch = output.match(
/app\/stage\d+\.test\.ts:[\s\S]+?Ran \d+ tests across \d+ files\./,
);
if (testRunMatch) {
return testRunMatch[0].trim();
if (output.includes("pytest")) {
// For Python tests, return the complete test output
return output.trim();
} else {
// Handle TypeScript test output (unchanged)
const testRunMatch = output.match(
/app\/stage\d+\.test\.ts:[\s\S]+?Ran \d+ tests across \d+ files\./,
);
if (testRunMatch) {
return testRunMatch[0].trim();
}
}
return output.trim();
}
17 changes: 17 additions & 0 deletions supportedLanguageDockerfiles/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM py-base

WORKDIR /app

# Copy requirements.txt if it exists
COPY requirements.txt* ./
# Install pytest and any other requirements
RUN pip install pytest \
&& if [ -f "requirements.txt" ]; then pip install -r requirements.txt; fi

# Copy the application code and tests
COPY . .

# Make the run script executable
RUN chmod +x /app/.hxckr/run.sh

CMD ["/app/.hxckr/run.sh"]

0 comments on commit 29fa715

Please sign in to comment.