New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-generate pants.ini with pinned pants_version if file is missing #45
Changes from all commits
b163f43
46ad8f5
5c2f975
7a83883
f8bae90
4268a3f
a5e6253
a109dbf
91b841e
9b10896
a70fb03
dc50bb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
"""Test that auto-generation of pants.ini works as expected. | ||
|
||
Note this is a separate file from ci.py because we only want to test the auto-generation | ||
part of ./pants here, and leave it to ci.py to test the other behavior like correctly | ||
parsing values from pants.ini and that the virtual env actually works. """ | ||
|
||
import configparser | ||
import subprocess | ||
import unittest | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
from ci import PantsVersion, setup_pants_version | ||
from common import (CONFIG_GLOBAL_SECTION, PANTS_INI, banner, read_config, | ||
temporarily_remove_config, travis_section) | ||
|
||
|
||
class TestPantsIniAutogen(unittest.TestCase): | ||
|
||
@contextmanager | ||
def autogen_pants_ini(self): | ||
banner("Temporarily removing pants.ini.") | ||
with temporarily_remove_config(): | ||
subprocess.run(["./pants"], check=True) | ||
yield | ||
banner("Restoring original pants.ini.") | ||
|
||
def test_file_created(self) -> None: | ||
with self.autogen_pants_ini(): | ||
self.assertTrue(Path(PANTS_INI).is_file()) | ||
|
||
|
||
def test_pants_versions_pinned_properly(self) -> None: | ||
with self.autogen_pants_ini(): | ||
config = read_config() | ||
self.assertIn("pants_version", config[CONFIG_GLOBAL_SECTION]) | ||
pinned_pants_version = config[CONFIG_GLOBAL_SECTION]["pants_version"] | ||
with setup_pants_version(PantsVersion.unspecified): | ||
unconfigured_pants_version = subprocess.run( | ||
["./pants", "-V"], stdout=subprocess.PIPE, encoding="utf-8", check=True | ||
).stdout.strip() | ||
self.assertEqual(pinned_pants_version, unconfigured_pants_version) | ||
|
||
|
||
if __name__ == "__main__": | ||
with travis_section("PantsIniAutoGen", "Testing auto-generation of pants.ini."): | ||
unittest.main() |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -52,6 +52,9 @@ function get_exe_path_or_die { | |||||
} | ||||||
|
||||||
function get_pants_ini_config_value { | ||||||
if [[ ! -f 'pants.ini' ]]; then | ||||||
return 0 | ||||||
fi | ||||||
config_key="$1" | ||||||
valid_delimiters="[:=]" | ||||||
optional_space="[[:space:]]*" | ||||||
|
@@ -74,6 +77,7 @@ EOF | |||||
# 2.) Grab pants version from pants.ini or default to latest. | ||||||
# 3.) Check for a venv via a naming/path convention and execute if found. | ||||||
# 4.) Otherwise create venv and re-exec self. | ||||||
# 5.) Check if pants.ini already exists. If not, create with sensible defaults then exit. | ||||||
# | ||||||
# After that pants itself will handle making sure any requested plugins | ||||||
# are installed and up to date. | ||||||
|
@@ -137,10 +141,26 @@ function bootstrap_pants { | |||||
echo "${PANTS_BOOTSTRAP}/${target_folder_name}" | ||||||
} | ||||||
|
||||||
function autogen_pants_ini { | ||||||
log "Config file \`pants.ini\` not detected in the repository root. Creating with sensible defaults:\n" | ||||||
touch "pants.ini" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to keep the touch because of the recursive call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, great! |
||||||
pants_version="$(./pants -V)" | ||||||
log "* Pinning \`pants_version\` to the most recent stable release: ${pants_version}." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After 1.15.0 is released, we'll also automatically pin
Will probably try to default to Python 3.6, then 3.7, then 2.7, so that dropping Py27 after 1.17.0 is a bit less surprising. TBD.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be even less surprising if we were to also mention in the logging to the user here that in 1.15.0 we will automatically pin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that might confuse people here, because there is nothing actionable for them to be able to do. We're only about 1-3 weeks away from 1.15.0, depending on how many RCs we have, so this will be fixed soon! |
||||||
cat <<EOF >> "pants.ini" | ||||||
[GLOBAL] | ||||||
pants_version: ${pants_version} | ||||||
EOF | ||||||
green "\n\`pants.ini\` created. You may now run \`./pants\`." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Within single quotes, nothing is substituted (which is why I try to use them where possible, to make it more clear when things are getting substituted into a string), so you can do things a little more fearlessly:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this suggestion, but then |
||||||
} | ||||||
|
||||||
# Ensure we operate from the context of the ./pants buildroot. | ||||||
cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" | ||||||
python="$(determine_pants_runtime_python_version)" | ||||||
pants_dir="$(bootstrap_pants "${python}")" | ||||||
if [[ ! -f 'pants.ini' ]]; then | ||||||
autogen_pants_ini | ||||||
exit 0 | ||||||
fi | ||||||
|
||||||
|
||||||
# We set the env var no_proxy to '*', to work around an issue with urllib using non | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was accidentally removed in #42.