test-windows.ps1 7.4 KB
Newer Older
1
2
3
4
5
6
function Assert-Output {
    param( [Parameter(Mandatory = $true)][bool]$success )
    if (-not $success) {
        $host.SetShouldExit(-1)
        exit 1
    }
7
8
}

9
$env:CONDA_ENV = "test-env"
10
$env:LGB_VER = (Get-Content $env:BUILD_SOURCESDIRECTORY\VERSION.txt).trim()
11
12
13
14
15
16
17
# Use custom temp directory to avoid
# > warning MSB8029: The Intermediate directory or Output directory cannot reside under the Temporary directory
# > as it could lead to issues with incremental build.
# And make sure this directory is always clean
$env:TMPDIR = "$env:USERPROFILE\tmp"
Remove-Item $env:TMPDIR -Force -Recurse -ErrorAction Ignore
[Void][System.IO.Directory]::CreateDirectory($env:TMPDIR)
18

19
20
21
# create the artifact upload directory if it doesn't exist yet
[Void][System.IO.Directory]::CreateDirectory($env:BUILD_ARTIFACTSTAGINGDIRECTORY)

22
if ($env:TASK -eq "r-package") {
23
    & .\.ci\test-r-package-windows.ps1 ; Assert-Output $?
24
    exit 0
25
26
}

27
if ($env:TASK -eq "cpp-tests") {
28
29
30
    cmake -B build -S . -DBUILD_CPP_TEST=ON -DUSE_DEBUG=ON -A x64
    cmake --build build --target testlightgbm --config Debug ; Assert-Output $?
    .\Debug\testlightgbm.exe ; Assert-Output $?
31
    exit 0
32
33
}

34
if ($env:TASK -eq "swig") {
35
36
    $env:JAVA_HOME = $env:JAVA_HOME_8_X64  # there is pre-installed Eclipse Temurin 8 somewhere
    $ProgressPreference = "SilentlyContinue"  # progress bar bug extremely slows down download speed
37
38
39
40
41
42
    $params = @{
        Uri = "https://sourceforge.net/projects/swig/files/latest/download"
        OutFile = "$env:BUILD_SOURCESDIRECTORY/swig/swigwin.zip"
        UserAgent = "curl"
    }
    Invoke-WebRequest @params
43
    Add-Type -AssemblyName System.IO.Compression.FileSystem
44
45
46
47
    [System.IO.Compression.ZipFile]::ExtractToDirectory(
        "$env:BUILD_SOURCESDIRECTORY/swig/swigwin.zip",
        "$env:BUILD_SOURCESDIRECTORY/swig"
    ) ; Assert-Output $?
48
    $SwigFolder = Get-ChildItem -Name -Path "$env:BUILD_SOURCESDIRECTORY/swig" -Attributes Directory
49
    $env:PATH = @("$env:BUILD_SOURCESDIRECTORY/swig/$SwigFolder", "$env:PATH") -join ";"
50
51
52
53
54
55
56
57
58
59
60
61
    $BuildLogFileName = "$env:BUILD_SOURCESDIRECTORY\cmake_build.log"
    cmake -B build -S . -A x64 -DUSE_SWIG=ON *> "$BuildLogFileName" ; $build_succeeded = $?
    Write-Output "CMake build logs:"
    Get-Content -Path "$BuildLogFileName"
    Assert-Output $build_succeeded
    $checks = Select-String -Path "${BuildLogFileName}" -Pattern "-- Found SWIG.*${SwigFolder}/swig.exe"
    $checks_cnt = $checks.Matches.length
    if ($checks_cnt -eq 0) {
        Write-Output "Wrong SWIG version was found (expected '${SwigFolder}'). Check the build logs."
        Assert-Output $False
    }
    cmake --build build --target ALL_BUILD --config Release ; Assert-Output $?
62
    if ($env:PRODUCES_ARTIFACTS -eq "true") {
63
64
        cp ./build/lightgbmlib.jar $env:BUILD_ARTIFACTSTAGINGDIRECTORY/lightgbmlib_win.jar ; Assert-Output $?
    }
65
    exit 0
66
67
}

68
69
70
71
# setup for Python
conda init powershell
conda activate
conda config --set always_yes yes --set changeps1 no
72
conda update -q -y conda "python=$env:PYTHON_VERSION[build=*_cp*]"
73

74
75
76
77
# print output of 'conda info', to help in submitting bug reports
Write-Output "conda info:"
conda info

78
79
if ($env:PYTHON_VERSION -eq "3.9") {
    $env:CONDA_REQUIREMENT_FILE = "$env:BUILD_SOURCESDIRECTORY/.ci/conda-envs/ci-core-py39.txt"
80
} else {
81
    $env:CONDA_REQUIREMENT_FILE = "$env:BUILD_SOURCESDIRECTORY/.ci/conda-envs/ci-core.txt"
82
83
}

84
85
86
87
$condaParams = @(
    "-y",
    "-n", "$env:CONDA_ENV",
    "--file", "$env:CONDA_REQUIREMENT_FILE",
88
    "python=$env:PYTHON_VERSION[build=*_cp*]"
89
90
)
conda create @condaParams ; Assert-Output $?
91

92
93
94
95
# print output of 'conda list', to help in submitting bug reports
Write-Output "conda list:"
conda list -n $env:CONDA_ENV

96
if ($env:TASK -ne "bdist") {
97
    conda activate $env:CONDA_ENV
98
}
99

100
Set-Location "$env:BUILD_SOURCESDIRECTORY"
101
if ($env:TASK -eq "regular") {
102
103
104
    cmake -B build -S . -A x64 ; Assert-Output $?
    cmake --build build --target ALL_BUILD --config Release ; Assert-Output $?
    sh ./build-python.sh install --precompile ; Assert-Output $?
105
106
107
    cp ./Release/lib_lightgbm.dll "$env:BUILD_ARTIFACTSTAGINGDIRECTORY"
    cp ./Release/lightgbm.exe "$env:BUILD_ARTIFACTSTAGINGDIRECTORY"
} elseif ($env:TASK -eq "sdist") {
108
109
    sh ./build-python.sh sdist ; Assert-Output $?
    sh ./.ci/check-python-dists.sh ./dist ; Assert-Output $?
110
111
    Set-Location dist; pip install @(Get-ChildItem *.gz) -v ; Assert-Output $?
} elseif ($env:TASK -eq "bdist") {
112
113
114
115
116
    # Import the Chocolatey profile module so that the RefreshEnv command
    # invoked below properly updates the current PowerShell session environment.
    $module = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
    Import-Module "$module" ; Assert-Output $?
    RefreshEnv
117

118
119
    Write-Output "Current OpenCL drivers:"
    Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Khronos\OpenCL\Vendors
120

121
    conda activate $env:CONDA_ENV
122
123
    # TODO: restore --integrated-opencl as part of https://github.com/microsoft/LightGBM/issues/6968
    sh "build-python.sh" bdist_wheel ; Assert-Output $?
124
    sh ./.ci/check-python-dists.sh ./dist ; Assert-Output $?
125
126
    Set-Location dist; pip install @(Get-ChildItem *py3-none-win_amd64.whl) ; Assert-Output $?
    cp @(Get-ChildItem *py3-none-win_amd64.whl) "$env:BUILD_ARTIFACTSTAGINGDIRECTORY"
127
} elseif (($env:APPVEYOR -eq "true") -and ($env:TASK -eq "python")) {
128
129
130
131
132
    if ($env:COMPILER -eq "MINGW") {
        sh ./build-python.sh install --mingw ; Assert-Output $?
    } else {
        sh ./build-python.sh install; Assert-Output $?
    }
133
134
}

135
if (($env:TASK -eq "sdist") -or (($env:APPVEYOR -eq "true") -and ($env:TASK -eq "python"))) {
136
    # cannot test C API with "sdist" task
137
    $tests = "$env:BUILD_SOURCESDIRECTORY/tests/python_package_test"
138
} else {
139
    $tests = "$env:BUILD_SOURCESDIRECTORY/tests"
140
141
}
if ($env:TASK -eq "bdist") {
142
    # Make sure we can do both CPU and GPU; see tests/python_package_test/test_dual.py
143
144
    # TODO: set LIGHTGBM_TEST_DUAL_CPU_GPU back to "1" as part of https://github.com/microsoft/LightGBM/issues/6968
    env:LIGHTGBM_TEST_DUAL_CPU_GPU = "0"
145
}
146

147
pytest $tests ; Assert-Output $?
148

149
if (($env:TASK -eq "regular") -or (($env:APPVEYOR -eq "true") -and ($env:TASK -eq "python"))) {
150
    Set-Location "$env:BUILD_SOURCESDIRECTORY/examples/python-guide"
151
    @("import matplotlib", "matplotlib.use('Agg')") + (Get-Content "plot_example.py") | Set-Content "plot_example.py"
152
153
154
155
156
    # Prevent interactive window mode
    (Get-Content "plot_example.py").replace(
        'graph.render(view=True)',
        'graph.render(view=False)'
    ) | Set-Content "plot_example.py"
157
    conda install -y -n $env:CONDA_ENV "h5py>=3.10" "ipywidgets>=8.1.2" "notebook>=7.1.2"
158
    # Run all examples
159
    foreach ($file in @(Get-ChildItem *.py)) {
160
161
162
163
164
165
166
        @(
            "import sys, warnings",
            -join @(
                "warnings.showwarning = lambda message, category, filename, lineno, file=None, line=None: ",
                "sys.stdout.write(warnings.formatwarning(message, category, filename, lineno, line))"
            )
        ) + (Get-Content $file) | Set-Content $file
167
        python $file ; Assert-Output $?
168
169
170
171
172
173
174
175
    }
    # Run all notebooks
    Set-Location "$env:BUILD_SOURCESDIRECTORY/examples/python-guide/notebooks"
    (Get-Content "interactive_plot_example.ipynb").replace(
        'INTERACTIVE = False',
        'assert False, \"Interactive mode disabled\"'
    ) | Set-Content "interactive_plot_example.ipynb"
    jupyter nbconvert --ExecutePreprocessor.timeout=180 --to notebook --execute --inplace *.ipynb ; Assert-Output $?
176
}