mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 10:21:59 +00:00
Fixed some bugs, added unit tests
This commit is contained in:
parent
26aabafe04
commit
bad1d83cee
94
setup.ps1
94
setup.ps1
|
@ -51,15 +51,10 @@ function Get-UserSelection {
|
||||||
if ([string]::IsNullOrEmpty($UserInput)) {
|
if ([string]::IsNullOrEmpty($UserInput)) {
|
||||||
$UserInput = $DefaultChoice
|
$UserInput = $DefaultChoice
|
||||||
}
|
}
|
||||||
|
$UserInput = $UserInput.ToUpper()
|
||||||
if ($AllowMultipleSelections) {
|
|
||||||
$Selections = $UserInput.Split(',') | ForEach-Object {
|
|
||||||
$_.Trim().ToUpper()
|
|
||||||
}
|
|
||||||
|
|
||||||
$ErrorMessage = "Invalid input: $Selection. Please enter letters within the valid range of options."
|
|
||||||
|
|
||||||
# Convert each Selection from letter to Index and validate
|
if ($AllowMultipleSelections) {
|
||||||
|
$Selections = $UserInput.Split(',') | ForEach-Object { $_.Trim() }
|
||||||
$SelectedIndices = @()
|
$SelectedIndices = @()
|
||||||
foreach ($Selection in $Selections) {
|
foreach ($Selection in $Selections) {
|
||||||
if ($Selection -match '^[A-Z]$') {
|
if ($Selection -match '^[A-Z]$') {
|
||||||
|
@ -68,20 +63,18 @@ function Get-UserSelection {
|
||||||
$SelectedIndices += $Index
|
$SelectedIndices += $Index
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Log $ErrorMessage -Level 'ERROR'
|
Write-Log "Invalid input: $Selection. Please enter letters within the valid range of options." -Level 'ERROR'
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Log $ErrorMessage -Level 'ERROR'
|
Write-Log "Invalid input: $Selection. Please enter a letter between A and Z." -Level 'ERROR'
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $SelectedIndices
|
return $SelectedIndices
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
# Convert the Selection from letter to Index and validate
|
|
||||||
if ($UserInput -match '^[A-Z]$') {
|
if ($UserInput -match '^[A-Z]$') {
|
||||||
$SelectedIndex = [int][char]$UserInput - [int][char]'A'
|
$SelectedIndex = [int][char]$UserInput - [int][char]'A'
|
||||||
if ($SelectedIndex -ge 0 -and $SelectedIndex -lt $Options.Length) {
|
if ($SelectedIndex -ge 0 -and $SelectedIndex -lt $Options.Length) {
|
||||||
|
@ -98,16 +91,12 @@ function Get-UserSelection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Exit-Script {
|
function Exit-Script {
|
||||||
param (
|
param (
|
||||||
[int]$ExitCode,
|
[int]$ExitCode,
|
||||||
[bool]$WaitForKeypress = $true
|
[bool]$WaitForKeypress = $true
|
||||||
)
|
)
|
||||||
|
|
||||||
# Disable virtual environment
|
|
||||||
deactivate
|
|
||||||
|
|
||||||
if ($ExitCode -ne 0) {
|
if ($ExitCode -ne 0) {
|
||||||
Write-Log "Script failed. Would you like to open the log file? (Y/N)" -Level 'PROMPT'
|
Write-Log "Script failed. Would you like to open the log file? (Y/N)" -Level 'PROMPT'
|
||||||
$openLog = Read-Host
|
$openLog = Read-Host
|
||||||
|
@ -128,10 +117,19 @@ function Test-PythonExecutable {
|
||||||
[string]$PythonExecutable
|
[string]$PythonExecutable
|
||||||
)
|
)
|
||||||
|
|
||||||
|
$DeactivateVenv = Join-Path $VenvDir "Scripts\Deactivate.bat"
|
||||||
|
if (Test-Path $DeactivateVenv) {
|
||||||
|
Write-Host "Deactivating virtual environment..."
|
||||||
|
& $DeactivateVenv
|
||||||
|
Write-Host "Virtual environment deactivated."
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Host "Deactivation script not found: $DeactivateVenv"
|
||||||
|
}
|
||||||
|
|
||||||
$PythonCmd = Get-Command $PythonExecutable -ErrorAction SilentlyContinue
|
$PythonCmd = Get-Command $PythonExecutable -ErrorAction SilentlyContinue
|
||||||
if ($PythonCmd) {
|
if ($PythonCmd) {
|
||||||
$Command = "$($PythonCmd.Source) --version 2>&1"
|
$VersionOutput = & $PythonCmd.Source --version 2>&1
|
||||||
$VersionOutput = Invoke-Expression $Command
|
|
||||||
if ($LASTEXITCODE -eq 0) {
|
if ($LASTEXITCODE -eq 0) {
|
||||||
$Version = $VersionOutput | Select-String -Pattern "Python (\d+\.\d+\.\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }
|
$Version = $VersionOutput | Select-String -Pattern "Python (\d+\.\d+\.\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }
|
||||||
Write-Log "Python version $Version found using executable '$PythonExecutable'."
|
Write-Log "Python version $Version found using executable '$PythonExecutable'."
|
||||||
|
@ -172,39 +170,59 @@ function Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Define the path to the Python executable in the virtual environment
|
# Define the path to the Python executable in the virtual environment
|
||||||
$VenvPython = "$VenvDir\Scripts\Activate.ps1"
|
$ActivateVenv = "$VenvDir\Scripts\Activate.ps1"
|
||||||
|
|
||||||
# Check if the virtual environment exists, if not, create it
|
# Check if the virtual environment exists, if not, create it
|
||||||
if (-Not (Test-Path $VenvPython)) {
|
if (-Not (Test-Path $ActivateVenv)) {
|
||||||
Write-Log "Virtual environment not found. Creating virtual environment..." -Level 'ERROR'
|
Write-Log "Virtual environment not found. Creating virtual environment..." -Level 'ERROR'
|
||||||
& $PythonExecutable -m venv $VenvName
|
& $PythonExecutable -m venv $VenvName 2>&1 | Out-File $LogFilePath -Append
|
||||||
if (-Not (Test-Path $VenvPython)) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Log "Failed to create virtual environment." -Level 'ERROR'
|
Write-Log "Failed to create virtual environment." -Level 'ERROR'
|
||||||
Exit-Script -exitCode 1
|
Exit-Script -exitCode 1
|
||||||
}
|
}
|
||||||
Write-Log "Virtual environment created successfully."
|
else {
|
||||||
|
Write-Log "Virtual environment created."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Activate the virtual environment and check if it was successful
|
||||||
|
Write-Log "Virtual environment found. Activating virtual environment..."
|
||||||
|
& $ActivateVenv 2>&1 | Out-File $LogFilePath -Append
|
||||||
|
# Check if virtual environment is activated
|
||||||
|
if ($env:VIRTUAL_ENV) {
|
||||||
|
Write-Host "Virtual environment is activated at: $($env:VIRTUAL_ENV)"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Log "Failed to activate virtual environment." -Level 'ERROR'
|
||||||
|
Exit-Script -exitCode 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Ensure pip
|
||||||
|
python -m ensurepip --default-pip 2>&1 | Out-File $LogFilePath -Append
|
||||||
|
|
||||||
# Pull latest updates only if the repository state is not dirty
|
# Pull latest updates only if the repository state is not dirty
|
||||||
Write-Log "Checking if the repository is clean..."
|
Write-Log "Checking if the repository is clean..."
|
||||||
$status = & "C:\Program Files\Git\cmd\git.exe" status --porcelain
|
$Status = & "C:\Program Files\Git\cmd\git.exe" status --porcelain
|
||||||
if ($status) {
|
if ($Status) {
|
||||||
Write-Log "Repository is dirty. Skipping pull."
|
Write-Log "Repository is dirty. Skipping pull."
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Log "Pulling latest updates..."
|
Write-Log "Pulling latest updates..."
|
||||||
& "C:\Program Files\Git\cmd\git.exe" pull | Out-File $LogFilePath -Append 2>&1
|
& "C:\Program Files\Git\cmd\git.exe" pull 2>&1 | Out-File $LogFilePath -Append
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Log "Failed to pull updates from Git." -Level 'ERROR'
|
Write-Log "Failed to pull updates from Git." -Level 'ERROR'
|
||||||
Exit-Script -exitCode 1
|
Exit-Script -exitCode 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (-not (Test-Path "$VenvDir\Lib\site-packages\talib")) {
|
if (-not (Test-Path "$VenvDir\Lib\site-packages\talib")) {
|
||||||
# Install TA-Lib using the virtual environment's pip
|
# Install TA-Lib using the virtual environment's pip
|
||||||
Write-Log "Installing TA-Lib using virtual environment's pip..."
|
Write-Log "Installing TA-Lib using virtual environment's pip..."
|
||||||
python -m pip install --find-links=build_helpers\ --prefer-binary TA-Lib | Out-File $LogFilePath -Append 2>&1
|
python -m pip install --find-links=build_helpers\ --prefer-binary TA-Lib 2>&1 | Out-File $LogFilePath -Append
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Log "Failed to install TA-Lib." -Level 'ERROR'
|
||||||
|
Exit-Script -exitCode 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Present options for requirement files
|
# Present options for requirement files
|
||||||
|
@ -212,25 +230,25 @@ function Main {
|
||||||
|
|
||||||
# Cache the selected requirement files
|
# Cache the selected requirement files
|
||||||
$SelectedRequirementFiles = @()
|
$SelectedRequirementFiles = @()
|
||||||
$PipInstallArguments = ""
|
$PipInstallArguments = @()
|
||||||
foreach ($Index in $SelectedIndices) {
|
foreach ($Index in $SelectedIndices) {
|
||||||
$FilePath = Join-Path $PSScriptRoot $RequirementFiles[$Index]
|
$RelativePath = $RequirementFiles[$Index]
|
||||||
if (Test-Path $FilePath) {
|
if (Test-Path $RelativePath) {
|
||||||
$SelectedRequirementFiles += $FilePath
|
$SelectedRequirementFiles += $RelativePath
|
||||||
$PipInstallArguments += " -r $FilePath"
|
$PipInstallArguments += "-r", $RelativePath # Add each flag and path as separate elements
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Write-Log "Requirement file not found: $FilePath" -Level 'ERROR'
|
Write-Log "Requirement file not found: $RelativePath" -Level 'ERROR'
|
||||||
Exit-Script -exitCode 1
|
Exit-Script -exitCode 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($PipInstallArguments -ne "") {
|
if ($PipInstallArguments.Count -ne 0) {
|
||||||
python -m pip install $PipInstallArguments
|
& pip install @PipInstallArguments # Use array splatting to pass arguments correctly
|
||||||
}
|
}
|
||||||
|
|
||||||
# Install freqtrade from setup using the virtual environment's Python
|
# Install freqtrade from setup using the virtual environment's Python
|
||||||
Write-Log "Installing freqtrade from setup..."
|
Write-Log "Installing freqtrade from setup..."
|
||||||
python -m pip install -e . | Out-File $LogFilePath -Append 2>&1
|
pip install -e . 2>&1 | Out-File $LogFilePath -Append
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Log "Failed to install freqtrade." -Level 'ERROR'
|
Write-Log "Failed to install freqtrade." -Level 'ERROR'
|
||||||
Exit-Script -exitCode 1
|
Exit-Script -exitCode 1
|
||||||
|
@ -243,7 +261,7 @@ function Main {
|
||||||
# User selected "Yes"
|
# User selected "Yes"
|
||||||
# Install freqtrade UI using the virtual environment's install-ui command
|
# Install freqtrade UI using the virtual environment's install-ui command
|
||||||
Write-Log "Installing freqtrade UI..."
|
Write-Log "Installing freqtrade UI..."
|
||||||
python 'freqtrade', 'install-ui' | Out-File $LogFilePath -Append 2>&1
|
python freqtrade install-ui 2>&1 | Out-File $LogFilePath -Append
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Log "Failed to install freqtrade UI." -Level 'ERROR'
|
Write-Log "Failed to install freqtrade UI." -Level 'ERROR'
|
||||||
Exit-Script -exitCode 1
|
Exit-Script -exitCode 1
|
||||||
|
|
|
@ -57,6 +57,13 @@ Describe "Setup and Tests" {
|
||||||
$Result | Should -Be 1
|
$Result | Should -Be 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It "Should return the correct index for a valid single selection" {
|
||||||
|
$Options = @("Option1", "Option2", "Option3")
|
||||||
|
Mock Read-Host { return "b" }
|
||||||
|
$Result = Get-UserSelection -prompt "Select an option" -options $Options
|
||||||
|
$Result | Should -Be 1
|
||||||
|
}
|
||||||
|
|
||||||
It "Should return the default choice when no input is provided" {
|
It "Should return the default choice when no input is provided" {
|
||||||
$Options = @("Option1", "Option2", "Option3")
|
$Options = @("Option1", "Option2", "Option3")
|
||||||
Mock Read-Host { return "" }
|
Mock Read-Host { return "" }
|
||||||
|
@ -97,6 +104,13 @@ Describe "Setup and Tests" {
|
||||||
|
|
||||||
Context "Multiple selections" {
|
Context "Multiple selections" {
|
||||||
It "Should handle valid input correctly" {
|
It "Should handle valid input correctly" {
|
||||||
|
Mock Read-Host { return "A, B, C" }
|
||||||
|
$Options = @("Option1", "Option2", "Option3")
|
||||||
|
$Indices = Get-UserSelection -prompt "Select options" -options $Options -defaultChoice "A"
|
||||||
|
$Indices | Should -Be @(0, 1, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should handle valid input without whitespace correctly" {
|
||||||
Mock Read-Host { return "A,B,C" }
|
Mock Read-Host { return "A,B,C" }
|
||||||
$Options = @("Option1", "Option2", "Option3")
|
$Options = @("Option1", "Option2", "Option3")
|
||||||
$Indices = Get-UserSelection -prompt "Select options" -options $Options -defaultChoice "A"
|
$Indices = Get-UserSelection -prompt "Select options" -options $Options -defaultChoice "A"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user