Skip to content
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

Update Validate-Formatting to use black output vs git diff #19625

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions scripts/devops_tasks/validate_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,34 @@
import os
import logging
import sys

from common_tasks import run_check_call
import subprocess

logging.getLogger().setLevel(logging.INFO)

root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
sdk_dir = os.path.join(root_dir, "sdk")

SWAGGER_FOLDER = "swagger"


def run_black(service_dir):
logging.info("Running black for {}".format(service_dir))

command = [sys.executable, "-m", "black", "-l", "120", "sdk/{}".format(service_dir)]
out = subprocess.Popen([sys.executable, "-m", "black", "-l", "120", "sdk/{}".format(service_dir)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd = root_dir
)

run_check_call(command, root_dir)
stdout,stderr = out.communicate()

if stderr:
raise RuntimeError("black ran into some trouble during its invocation: " + stderr)

def check_diff(folder):
# We don't care about changes to txt files (dev_requirements change)
run_check_call(["git", "status"], sdk_dir, always_exit=False)
if stdout:
if "reformatted" in stdout.decode('utf-8'):
return False

dir_changed = folder.split("/")[:-2]
command = [
"git",
"diff",
"--exit-code",
"{}".format("/".join(dir_changed)),
]
result = run_check_call(command, sdk_dir, always_exit=False)
if result:
command = ["git", "status"]
run_check_call(command, root_dir)
raise ValueError(
"Found difference between formatted code and current commit. Please re-generate with the latest autorest."
)
return True


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run black to verify formatted code."
Expand All @@ -61,7 +51,8 @@ def check_diff(folder):

args = parser.parse_args()
if args.validate != "False":
run_black(args.service_directory)
check_diff("sdk/{}".format(args.service_directory))
if not run_black(args.service_directory):
raise ValueError("Found difference between formatted code and current commit. Please re-generate with the latest autorest.")

else:
print("Skipping formatting validation")