Skip to content

Commit f331714

Browse files
committed
test integration test
1 parent ee55633 commit f331714

File tree

2 files changed

+160
-11
lines changed

2 files changed

+160
-11
lines changed
Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test MetaTrader5 Integration
1+
name: Test MetaTrader5 Initialization
22

33
on:
44
push:
@@ -8,6 +8,7 @@ on:
88
jobs:
99
test:
1010
runs-on: windows-latest
11+
timeout-minutes: 15
1112

1213
steps:
1314
- uses: actions/checkout@v4
@@ -22,35 +23,57 @@ jobs:
2223
# Download MT5 setup
2324
Invoke-WebRequest -Uri "https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe" -OutFile mt5setup.exe
2425
25-
# Install MT5 silently
26+
# Install MT5 silently with increased timeout
2627
Start-Process -FilePath .\mt5setup.exe -ArgumentList "/auto" -Wait
2728
28-
# Verify installation
29+
# Verify installation and attempt to fix any issues
2930
$mtPath = "C:\Program Files\MetaTrader 5\terminal64.exe"
3031
if (Test-Path $mtPath) {
3132
Write-Host "MetaTrader 5 installed successfully"
3233
Write-Host "MetaTrader 5 found at: $mtPath"
3334
35+
# Set explicit permissions to ensure we can access files
36+
icacls "C:\Program Files\MetaTrader 5" /grant:r "Everyone:(OI)(CI)F" /T
37+
38+
# Set up environment for headless operation
39+
$terminalDir = "$env:APPDATA\MetaQuotes\Terminal"
40+
if (!(Test-Path $terminalDir)) {
41+
New-Item -Path $terminalDir -ItemType Directory -Force
42+
Write-Host "Created terminal directory: $terminalDir"
43+
}
44+
3445
# List installation directory for debugging
3546
Write-Host "Listing MetaTrader 5 installation directory:"
3647
Get-ChildItem "C:\Program Files\MetaTrader 5"
3748
} else {
3849
Write-Error "MetaTrader 5 installation failed"
3950
exit 1
4051
}
41-
42-
# Start MT5 in portable mode and give it time to initialize
43-
$mtProcess = Start-Process -FilePath $mtPath -ArgumentList "/portable" -PassThru
44-
$processId = $mtProcess.Id
45-
Write-Host "Started MetaTrader 5 terminal with process ID: $processId"
46-
Start-Sleep -Seconds 30
4752
4853
- name: Install Python dependencies
4954
run: |
5055
python -m pip install --upgrade pip
5156
pip install MetaTrader5
5257
53-
- name: Test MetaTrader5 connection with improved script
58+
- name: Run headless MetaTrader5 initialization test
59+
env:
60+
# Set any environment variables that might help with headless mode
61+
MT5_HEADLESS: "1"
5462
run: |
55-
python tests/integration/test_mt5_connection.py
63+
# Check running processes before test
64+
Write-Host "Currently running processes before test:"
65+
Get-Process | Where-Object { $_.ProcessName -like "*terminal*" -or $_.ProcessName -like "*meta*" } | Format-Table -AutoSize
66+
67+
# Run the headless initialization test
68+
python tests/integration/test_headless_initialize.py
69+
70+
# Store the exit code
71+
$exitCode = $LASTEXITCODE
72+
73+
# Check processes after test
74+
Write-Host "Currently running processes after test:"
75+
Get-Process | Where-Object { $_.ProcessName -like "*terminal*" -or $_.ProcessName -like "*meta*" } | Format-Table -AutoSize
76+
77+
# Return the exit code from the test
78+
exit $exitCode
5679
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""Test MetaTrader5 initialization in headless environments like GitHub CI."""
2+
3+
import os
4+
import sys
5+
import time
6+
import subprocess
7+
import MetaTrader5 as mt5
8+
9+
def setup_headless_environment():
10+
"""Prepare the environment for headless MT5 operation."""
11+
# Create required directories that might be expected by MT5
12+
base_path = os.path.expanduser("~")
13+
mt5_data_path = os.path.join(base_path, "AppData", "Roaming", "MetaQuotes", "Terminal")
14+
15+
os.makedirs(mt5_data_path, exist_ok=True)
16+
print(f"Created MT5 data directory: {mt5_data_path}")
17+
18+
# Set environment variables that might help with headless operation
19+
os.environ["MT5_HEADLESS"] = "1"
20+
return True
21+
22+
def test_headless_initialize():
23+
"""Test MT5 initialization in a headless environment."""
24+
print(f"MetaTrader5 package version: {mt5.__version__}")
25+
26+
# Setup headless environment
27+
setup_headless_environment()
28+
29+
# Start MT5 terminal process with headless-friendly options
30+
mt_path = "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
31+
if not os.path.exists(mt_path):
32+
print(f"ERROR: MetaTrader5 executable not found at {mt_path}")
33+
return False
34+
35+
print(f"Found MetaTrader5 at: {mt_path}")
36+
37+
# Kill any existing MT5 processes
38+
try:
39+
subprocess.run(["taskkill", "/F", "/IM", "terminal64.exe"],
40+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41+
print("Killed any existing MetaTrader 5 processes")
42+
time.sleep(2)
43+
except Exception as e:
44+
print(f"Note: No existing processes to kill: {e}")
45+
46+
# Launch with multiple flags for headless operation
47+
try:
48+
print("Starting MetaTrader 5 terminal with headless-friendly options...")
49+
process = subprocess.Popen([
50+
mt_path,
51+
"/portable", # Use portable mode to avoid requiring login
52+
"/skipupdate", # Skip checking for updates
53+
"/config:headless", # Use a specific config name
54+
"/nogui" # No GUI needed (might not be supported but worth trying)
55+
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56+
57+
print(f"Started MetaTrader 5 with PID: {process.pid}")
58+
time.sleep(15) # Give it time to initialize backend services
59+
except Exception as e:
60+
print(f"Error starting MetaTrader 5: {e}")
61+
return False
62+
63+
# Try initialization methods in sequence
64+
print("\nAttempting MT5 initialization...")
65+
66+
# Method 1: With path parameter and longer timeout
67+
print("Method 1: With explicit path and timeout")
68+
try:
69+
result = mt5.initialize(path=mt_path, timeout=60000) # 60-second timeout
70+
error = mt5.last_error()
71+
print(f"Result: {result}, Error: {error}")
72+
if result:
73+
print("Method 1 SUCCESS!")
74+
terminal_info = mt5.terminal_info()
75+
if terminal_info:
76+
print(f"Terminal info - Path: {terminal_info.path}, Connected: {getattr(terminal_info, 'connected', 'N/A')}")
77+
mt5.shutdown()
78+
return True
79+
except Exception as e:
80+
print(f"Exception in method 1: {e}")
81+
82+
# Method 2: With server parameter (try to avoid login)
83+
print("\nMethod 2: With server parameter")
84+
try:
85+
result = mt5.initialize(
86+
path=mt_path,
87+
server="Demo", # Use demo server
88+
login=0, # No login
89+
timeout=60000 # 60-second timeout
90+
)
91+
error = mt5.last_error()
92+
print(f"Result: {result}, Error: {error}")
93+
if result:
94+
print("Method 2 SUCCESS!")
95+
mt5.shutdown()
96+
return True
97+
except Exception as e:
98+
print(f"Exception in method 2: {e}")
99+
100+
# Method 3: Basic initialization with longer wait after process start
101+
print("\nMethod 3: After additional delay")
102+
try:
103+
print("Waiting 30 more seconds for MT5 to fully initialize...")
104+
time.sleep(30)
105+
result = mt5.initialize()
106+
error = mt5.last_error()
107+
print(f"Result: {result}, Error: {error}")
108+
if result:
109+
print("Method 3 SUCCESS!")
110+
mt5.shutdown()
111+
return True
112+
except Exception as e:
113+
print(f"Exception in method 3: {e}")
114+
115+
# If we got here, all methods failed
116+
print("\nAll initialization attempts failed in headless mode")
117+
return False
118+
119+
if __name__ == "__main__":
120+
success = test_headless_initialize()
121+
if success:
122+
print("\nHEADLESS TEST PASSED: Successfully initialized MT5")
123+
sys.exit(0)
124+
else:
125+
print("\nHEADLESS TEST FAILED: Could not initialize MT5")
126+
sys.exit(1)

0 commit comments

Comments
 (0)