|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simple test showing how users will use GitEnv.from_docker_image(). |
| 4 | +
|
| 5 | +This is the simplest possible usage. |
| 6 | +
|
| 7 | +Prerequisites: |
| 8 | + 1. .env file configured (copy from .env.example) |
| 9 | + 2. Shared Gitea running: ./scripts/setup_shared_gitea.sh |
| 10 | + 3. OpenEnv repo migrated to Gitea (see README) |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +# Load environment variables from .env file |
| 18 | +from dotenv import load_dotenv |
| 19 | +load_dotenv() |
| 20 | + |
| 21 | +# Add src to path |
| 22 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 23 | + |
| 24 | +from envs.git_env import GitAction, GitEnv |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + """Test GitEnv.from_docker_image().""" |
| 29 | + print("=" * 60) |
| 30 | + print("GitEnv.from_docker_image() Test") |
| 31 | + print("=" * 60) |
| 32 | + print() |
| 33 | + |
| 34 | + try: |
| 35 | + # Pass environment variables from .env to container |
| 36 | + env_vars = { |
| 37 | + "GITEA_URL": os.getenv("GITEA_URL"), |
| 38 | + "GITEA_USERNAME": os.getenv("GITEA_USERNAME"), |
| 39 | + "GITEA_PASSWORD": os.getenv("GITEA_PASSWORD"), |
| 40 | + } |
| 41 | + |
| 42 | + # Verify env vars are loaded |
| 43 | + if not all(env_vars.values()): |
| 44 | + print("❌ Error: Required environment variables not found in .env") |
| 45 | + print(" Make sure .env file exists (copy from .env.example)") |
| 46 | + return False |
| 47 | + |
| 48 | + print("Creating client from Docker image with .env credentials...") |
| 49 | + print(" Using GitEnv.from_docker_image() factory method") |
| 50 | + print() |
| 51 | + |
| 52 | + # Create client using from_docker_image factory method |
| 53 | + client = GitEnv.from_docker_image("git-env:latest", env_vars=env_vars) |
| 54 | + |
| 55 | + print("✓ Client created and container started!\n") |
| 56 | + |
| 57 | + # Now use it like any other client |
| 58 | + print("Testing the environment:") |
| 59 | + print("-" * 60) |
| 60 | + |
| 61 | + # Reset |
| 62 | + print("\n1. Reset:") |
| 63 | + result = client.reset() |
| 64 | + print(f" Message: {result.observation.message}") |
| 65 | + print(f" Success: {result.observation.success}") |
| 66 | + |
| 67 | + # Get initial state |
| 68 | + state = client.state() |
| 69 | + print(f" State: episode_id={state.episode_id}, step_count={state.step_count}") |
| 70 | + print(f" Gitea ready: {state.gitea_ready}") |
| 71 | + |
| 72 | + # List repositories |
| 73 | + print("\n2. List repositories:") |
| 74 | + result = client.step(GitAction(action_type="list_repos")) |
| 75 | + print(f" Success: {result.observation.success}") |
| 76 | + print(f" Found {len(result.observation.repos)} repositories") |
| 77 | + for repo in result.observation.repos: |
| 78 | + print(f" - {repo['name']}") |
| 79 | + |
| 80 | + # Clone repository |
| 81 | + print("\n3. Clone repository:") |
| 82 | + result = client.step(GitAction(action_type="clone_repo", repo_name="OpenEnv")) |
| 83 | + print(f" Success: {result.observation.success}") |
| 84 | + print(f" Message: {result.observation.message}") |
| 85 | + print(f" Output: {result.observation.output}") |
| 86 | + |
| 87 | + # Execute git commands |
| 88 | + print("\n4. Execute git commands:") |
| 89 | + |
| 90 | + git_commands = [ |
| 91 | + "status", |
| 92 | + "log --oneline -5", |
| 93 | + "branch -a", |
| 94 | + ] |
| 95 | + |
| 96 | + for cmd in git_commands: |
| 97 | + result = client.step( |
| 98 | + GitAction(action_type="execute_git_command", command=cmd, working_dir="OpenEnv") |
| 99 | + ) |
| 100 | + print(f"\n git {cmd}:") |
| 101 | + print(f" Success: {result.observation.success}") |
| 102 | + if result.observation.output: |
| 103 | + # Show first few lines |
| 104 | + lines = result.observation.output.strip().split("\n")[:5] |
| 105 | + for line in lines: |
| 106 | + print(f" {line}") |
| 107 | + if len(result.observation.output.strip().split("\n")) > 5: |
| 108 | + print(" ...") |
| 109 | + |
| 110 | + # Check final state |
| 111 | + print("\n5. Check final state:") |
| 112 | + state = client.state() |
| 113 | + print(f" episode_id: {state.episode_id}") |
| 114 | + print(f" step_count: {state.step_count}") |
| 115 | + print(f" gitea_ready: {state.gitea_ready}") |
| 116 | + |
| 117 | + print("\n" + "-" * 60) |
| 118 | + print("\n✓ All operations successful!") |
| 119 | + print() |
| 120 | + |
| 121 | + print("Cleaning up...") |
| 122 | + client.close() |
| 123 | + print("✓ Container stopped and removed") |
| 124 | + print() |
| 125 | + |
| 126 | + print("=" * 60) |
| 127 | + print("Test completed successfully!") |
| 128 | + print("=" * 60) |
| 129 | + |
| 130 | + return True |
| 131 | + |
| 132 | + except Exception as e: |
| 133 | + print(f"\n❌ Test failed: {e}") |
| 134 | + import traceback |
| 135 | + |
| 136 | + traceback.print_exc() |
| 137 | + return False |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + success = main() |
| 142 | + exit(0 if success else 1) |
0 commit comments