Spaces:
Running
Running
File size: 5,731 Bytes
fd0524b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
#!/usr/bin/env python3
"""
Test script to verify environment variables are properly set in virtual environment
"""
import os
import sys
import subprocess
from pathlib import Path
def test_environment_variables():
"""Test that environment variables are properly set"""
print("π Testing Environment Variables")
print("=" * 50)
# Test token from user
test_token = "xxxxx"
# Set environment variables
os.environ['HF_TOKEN'] = test_token
os.environ['HUGGING_FACE_HUB_TOKEN'] = test_token
os.environ['HF_USERNAME'] = 'Tonic'
os.environ['TRACKIO_DATASET_REPO'] = 'Tonic/trackio-experiments'
print(f"Testing environment setup with token: {'*' * 10}...{test_token[-4:]}")
# Check if environment variables are set
required_vars = ['HF_TOKEN', 'HUGGING_FACE_HUB_TOKEN', 'HF_USERNAME', 'TRACKIO_DATASET_REPO']
all_set = True
for var in required_vars:
value = os.environ.get(var)
if value:
print(f"[OK] {var}: {value[:10] if len(value) > 10 else value}...{value[-4:] if len(value) > 4 else ''}")
else:
print(f"[ERROR] {var}: Not set")
all_set = False
return all_set
def test_virtual_environment():
"""Test that virtual environment can access environment variables"""
print("\nπ Testing Virtual Environment Access")
print("=" * 50)
# Test token from user
test_token = "xxxx"
# Create a simple Python script to test environment variables
test_script = """
import os
import sys
# Check environment variables
required_vars = ['HF_TOKEN', 'HUGGING_FACE_HUB_TOKEN', 'HF_USERNAME', 'TRACKIO_DATASET_REPO']
print("Environment variables in virtual environment:")
all_set = True
for var in required_vars:
value = os.environ.get(var)
if value:
print(f"[OK] {var}: {value[:10] if len(value) > 10 else value}...{value[-4:] if len(value) > 4 else ''}")
else:
print(f"[ERROR] {var}: Not set")
all_set = False
if all_set:
print("\\n[OK] All environment variables are properly set in virtual environment")
sys.exit(0)
else:
print("\\n[ERROR] Some environment variables are missing in virtual environment")
sys.exit(1)
"""
# Write test script to temporary file
test_file = Path("tests/temp_env_test.py")
test_file.write_text(test_script)
try:
# Set environment variables
env = os.environ.copy()
env['HF_TOKEN'] = test_token
env['HUGGING_FACE_HUB_TOKEN'] = test_token
env['HF_USERNAME'] = 'Tonic'
env['TRACKIO_DATASET_REPO'] = 'Tonic/trackio-experiments'
# Run the test script
result = subprocess.run([sys.executable, str(test_file)],
env=env,
capture_output=True,
text=True)
print(result.stdout)
if result.stderr:
print(f"Errors: {result.stderr}")
return result.returncode == 0
finally:
# Clean up
if test_file.exists():
test_file.unlink()
def test_launch_script_environment():
"""Test that launch script properly sets environment variables"""
print("\nπ Testing Launch Script Environment Setup")
print("=" * 50)
# Check if launch.sh exists
launch_script = Path("launch.sh")
if not launch_script.exists():
print("β launch.sh not found")
return False
# Read launch script and check for environment variable exports
script_content = launch_script.read_text()
required_exports = [
'export HF_TOKEN=',
'export HUGGING_FACE_HUB_TOKEN=',
'export HF_USERNAME=',
'export TRACKIO_DATASET_REPO='
]
all_found = True
for export in required_exports:
if export in script_content:
print(f"[OK] Found: {export}")
else:
print(f"[ERROR] Missing: {export}")
all_found = False
# Check for virtual environment activation
if 'source smollm3_env/bin/activate' in script_content:
print("[OK] Found virtual environment activation")
else:
print("[ERROR] Missing virtual environment activation")
all_found = False
# Check for environment variable re-export after activation
if 'export HF_TOKEN="$HF_TOKEN"' in script_content:
print("[OK] Found environment variable re-export after activation")
else:
print("[ERROR] Missing environment variable re-export after activation")
all_found = False
return all_found
def main():
"""Run all environment tests"""
print("π Environment Setup Verification")
print("=" * 50)
tests = [
test_environment_variables,
test_virtual_environment,
test_launch_script_environment
]
all_passed = True
for test in tests:
try:
if not test():
all_passed = False
except Exception as e:
print(f"β Test failed with error: {e}")
all_passed = False
print("\n" + "=" * 50)
if all_passed:
print("[SUCCESS] ALL ENVIRONMENT TESTS PASSED!")
print("[OK] Environment variables: Properly set")
print("[OK] Virtual environment: Can access variables")
print("[OK] Launch script: Properly configured")
print("\nThe environment setup is working correctly!")
else:
print("[ERROR] SOME ENVIRONMENT TESTS FAILED!")
print("Please check the failed tests above.")
return all_passed
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1) |