fix UnboundLocalError instead of proper error message (#221)
* fix UnboundLocalError: local variable 'platform' referenced before assignment * add test to protect against regression
This commit is contained in:
committed by
Matthieu Darbois
parent
09e72800c6
commit
d2700bc3b4
+15
-10
@@ -61,25 +61,30 @@ def main():
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
detect_obsolete_options()
|
detect_obsolete_options()
|
||||||
|
|
||||||
if args.platform != 'auto':
|
if args.platform != 'auto':
|
||||||
platform = args.platform
|
platform = args.platform
|
||||||
else:
|
else:
|
||||||
ci = strtobool(os.environ.get('CI', 'false')) or 'BITRISE_BUILD_NUMBER' in os.environ or 'AZURE_HTTP_USER_AGENT' in os.environ
|
ci = strtobool(os.environ.get('CI', 'false')) or 'BITRISE_BUILD_NUMBER' in os.environ or 'AZURE_HTTP_USER_AGENT' in os.environ
|
||||||
if ci:
|
if not ci:
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
platform = 'linux'
|
|
||||||
elif sys.platform == 'darwin':
|
|
||||||
platform = 'macos'
|
|
||||||
elif sys.platform == 'win32':
|
|
||||||
platform = 'windows'
|
|
||||||
if platform is None:
|
|
||||||
print('cibuildwheel: Unable to detect platform. cibuildwheel should run on your CI server, '
|
print('cibuildwheel: Unable to detect platform. cibuildwheel should run on your CI server, '
|
||||||
'Travis CI, AppVeyor, Azure Pipelines and CircleCI are supported. You can run on your '
|
'Travis CI, AppVeyor, Azure Pipelines and CircleCI are supported. You can run on your '
|
||||||
'development machine or other CI providers using the --platform argument. Check --help '
|
'development machine or other CI providers using the --platform argument. Check --help '
|
||||||
'output for more information.',
|
'output for more information.',
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
exit(2)
|
exit(2)
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
platform = 'linux'
|
||||||
|
elif sys.platform == 'darwin':
|
||||||
|
platform = 'macos'
|
||||||
|
elif sys.platform == 'win32':
|
||||||
|
platform = 'windows'
|
||||||
|
else:
|
||||||
|
print('cibuildwheel: Unable to detect platform from "sys.platform" in a CI environment. You can run '
|
||||||
|
'cibuildwheel using the --platform argument. Check --help output for more information.',
|
||||||
|
file=sys.stderr)
|
||||||
|
exit(2)
|
||||||
|
|
||||||
|
|
||||||
output_dir = args.output_dir
|
output_dir = args.output_dir
|
||||||
test_command = get_option_from_environment('CIBW_TEST_COMMAND', platform=platform)
|
test_command = get_option_from_environment('CIBW_TEST_COMMAND', platform=platform)
|
||||||
@@ -174,7 +179,7 @@ def main():
|
|||||||
elif platform == 'macos':
|
elif platform == 'macos':
|
||||||
cibuildwheel.macos.build(**build_options)
|
cibuildwheel.macos.build(**build_options)
|
||||||
else:
|
else:
|
||||||
raise Exception('Unsupported platform')
|
raise Exception('Unsupported platform: {}'.format(platform))
|
||||||
|
|
||||||
|
|
||||||
def detect_obsolete_options():
|
def detect_obsolete_options():
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from cibuildwheel.__main__ import main
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_platform_non_ci(monkeypatch, capsys):
|
||||||
|
monkeypatch.setattr(os, 'environ', {})
|
||||||
|
monkeypatch.setattr(sys, "argv", ["python", "."])
|
||||||
|
with pytest.raises(SystemExit) as exit:
|
||||||
|
main()
|
||||||
|
assert exit.value.code == 2
|
||||||
|
_, err = capsys.readouterr()
|
||||||
|
assert 'cibuildwheel: Unable to detect platform.' in err
|
||||||
|
assert "cibuildwheel should run on your CI server" in err
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_platform_on_ci(monkeypatch, capsys):
|
||||||
|
monkeypatch.setattr(os, 'environ', {"CI": "true"})
|
||||||
|
monkeypatch.setattr(sys, "argv", ["python", "."])
|
||||||
|
|
||||||
|
monkeypatch.setattr(sys, "platform", "Something")
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit) as exit:
|
||||||
|
main()
|
||||||
|
_, err = capsys.readouterr()
|
||||||
|
assert exit.value.code == 2
|
||||||
|
assert 'cibuildwheel: Unable to detect platform from "sys.platform"' in err
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_platform(monkeypatch):
|
||||||
|
monkeypatch.setattr(os, 'environ', {"CIBW_PLATFORM": "Something"})
|
||||||
|
monkeypatch.setattr(sys, "argv", ["python", "."])
|
||||||
|
|
||||||
|
with pytest.raises(Exception) as exc:
|
||||||
|
main()
|
||||||
|
assert exc.value.args[0] == 'Unsupported platform: Something'
|
||||||
Reference in New Issue
Block a user