Added testing and code checks
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# Test-ObsidianInstallation.ps1
|
||||
|
||||
function Test-AdminPrivilege {
|
||||
if (-Not (Test-Administrator)) {
|
||||
Write-Output "Running as non-admin user for testing admin check..."
|
||||
} else {
|
||||
Write-Output "Running as admin user for testing admin check..."
|
||||
}
|
||||
}
|
||||
|
||||
function Test-ChocolateyInstallation {
|
||||
Write-Output "Simulating environment without Chocolatey..."
|
||||
Remove-Item -Path "C:\ProgramData\chocolatey" -Recurse -Force -ErrorAction SilentlyContinue
|
||||
|
||||
if (-Not (Get-Command choco -ErrorAction SilentlyContinue)) {
|
||||
Write-Output "Chocolatey is not installed. Proceeding with installation..."
|
||||
} else {
|
||||
Write-Output "Chocolatey is already installed."
|
||||
}
|
||||
}
|
||||
|
||||
function Test-ObsidianDownload {
|
||||
Write-Output "Simulating download failure..."
|
||||
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name "ProxyEnable" -Value 1
|
||||
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name "ProxyServer" -Value "127.0.0.1:8080"
|
||||
Start-Sleep -Seconds 5
|
||||
|
||||
Write-Output "Downloading Obsidian installer..."
|
||||
wget https://github.com/obsidianmd/obsidian-releases/releases/download/v1.6.3/Obsidian.1.6.3.exe -OutFile ObsidianInstaller.exe
|
||||
|
||||
if (Test-Path "ObsidianInstaller.exe") {
|
||||
Write-Output "Download succeeded unexpectedly."
|
||||
} else {
|
||||
Write-Output "Download failed as expected."
|
||||
}
|
||||
|
||||
# Clean up proxy settings
|
||||
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name "ProxyEnable" -Value 0
|
||||
}
|
||||
|
||||
# Simulate invalid plugins.json file
|
||||
Set-Content -Path "invalid_plugins.json" -Value @"
|
||||
{
|
||||
"core_plugins": ["plugin1", "plugin2"]
|
||||
"community_plugins": ["plugin3", "plugin4"]
|
||||
}
|
||||
"@
|
||||
|
||||
function Test-InvalidPluginConfig {
|
||||
Write-Output "Testing with invalid plugins.json..."
|
||||
try {
|
||||
$pluginsConfig = Get-Content -Raw -Path "invalid_plugins.json" | ConvertFrom-Json
|
||||
} catch {
|
||||
Write-Output "Caught error parsing invalid plugins.json as expected: $_"
|
||||
}
|
||||
}
|
||||
|
||||
# Run tests
|
||||
Test-AdminPrivilege
|
||||
Test-ChocolateyInstallation
|
||||
Test-ObsidianDownload
|
||||
Test-InvalidPluginConfig
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/bin/bash
|
||||
|
||||
function test_os_selection {
|
||||
echo "Testing OS selection..."
|
||||
echo "L" | ./install_obsidian.sh
|
||||
echo "M" | ./install_obsidian.sh
|
||||
echo "W" | ./install_obsidian.sh
|
||||
}
|
||||
|
||||
function test_dependencies_installation {
|
||||
echo "Simulating environment without wget and jq..."
|
||||
sudo apt remove -y wget jq
|
||||
|
||||
echo "Installing dependencies..."
|
||||
sudo apt update
|
||||
sudo apt install -y wget jq
|
||||
|
||||
if ! command -v wget &> /dev/null || ! command -v jq &> /dev/null; then
|
||||
echo "Failed to install dependencies."
|
||||
else
|
||||
echo "Dependencies installed successfully."
|
||||
fi
|
||||
}
|
||||
|
||||
function test_obsidian_download_failure {
|
||||
echo "Simulating download failure..."
|
||||
sudo iptables -A OUTPUT -p tcp --dport 443 -j REJECT
|
||||
|
||||
echo "Downloading Obsidian..."
|
||||
wget https://github.com/obsidianmd/obsidian-releases/releases/download/v1.6.3/Obsidian-1.6.3.AppImage -O obsidian.AppImage
|
||||
if [ ! -f obsidian.AppImage ]; then
|
||||
echo "Download failed as expected."
|
||||
else
|
||||
echo "Download succeeded unexpectedly."
|
||||
fi
|
||||
|
||||
# Clean up iptables rule
|
||||
sudo iptables -D OUTPUT -p tcp --dport 443 -j REJECT
|
||||
}
|
||||
|
||||
# Create an invalid plugins.json file
|
||||
echo '{
|
||||
"core_plugins": ["plugin1", "plugin2"]
|
||||
"community_plugins": ["plugin3", "plugin4"]
|
||||
' > invalid_plugins.json
|
||||
|
||||
function test_invalid_plugins_config {
|
||||
echo "Testing with invalid plugins.json..."
|
||||
if jq . invalid_plugins.json; then
|
||||
echo "Invalid plugins.json parsed unexpectedly."
|
||||
else
|
||||
echo "Caught error parsing invalid plugins.json as expected."
|
||||
fi
|
||||
}
|
||||
|
||||
# Run tests
|
||||
test_os_selection
|
||||
test_dependencies_installation
|
||||
test_obsidian_download_failure
|
||||
test_invalid_plugins_config
|
||||
@@ -11,7 +11,8 @@ function Install-Obsidian-Windows {
|
||||
Write-Output "Chocolatey is not installed. Installing Chocolatey..."
|
||||
Set-ExecutionPolicy Bypass -Scope Process -Force;
|
||||
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
|
||||
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'));
|
||||
$script = (New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')
|
||||
Invoke-Command -ScriptBlock ([ScriptBlock]::Create($script))
|
||||
}
|
||||
choco install -y wget jq
|
||||
|
||||
@@ -36,7 +37,7 @@ function Install-Obsidian-Windows {
|
||||
}
|
||||
}
|
||||
|
||||
function Install-Plugins {
|
||||
function Install-Plugin {
|
||||
Write-Output "Installing plugins..."
|
||||
|
||||
# Kill Obsidian process if running
|
||||
@@ -79,4 +80,4 @@ if (-Not (Test-Administrator)) {
|
||||
|
||||
# Directly run Windows installation and plugin configuration
|
||||
Install-Obsidian-Windows
|
||||
Install-Plugins
|
||||
Install-Plugin
|
||||
|
||||
Reference in New Issue
Block a user