From 6a0b1f26752236ec5ba9740bf72ad0326141a2cb Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 28 Jan 2020 17:21:02 +0100 Subject: [PATCH] Refactor the untracked files check Co-Authored-By: Pradyun Gedam --- noxfile.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/noxfile.py b/noxfile.py index 7a7e707fa..bf603eaba 100644 --- a/noxfile.py +++ b/noxfile.py @@ -262,19 +262,28 @@ def build_dists(session): session.log( "# Check if there's any Git-untracked files before building the wheel", ) - has_git_untracked_files = any( - bool(l) for l in - # session.run doesn't seem to return any output - subprocess.check_output( - ( - "git", "ls-files", "--ignored", "--exclude-standard", - "--others", "--", ".", - ), - text=True, - ).split('\n') - if not l.startswith('.nox/build-release/') # exclude nox env file + + def get_git_untracked_files(): + """List all local file paths that aren't tracked by Git.""" + git_ls_files_cmd = ( + "git", "ls-files", "--ignored", "--exclude-standard", + "--others", "--", ".", + ) + # session.run doesn't seem to return any output: + ls_files_out = subprocess.check_output(git_ls_files_cmd, text=True) + ls_files_out_lines = ls_files_out.splitlines() + for file_name in ls_files_out_lines: + if file_name.strip(): # it's useless if empty + continue + + yield file_name + + has_forbidden_git_untracked_files = any( + # Don't report the environment this session is running in + not untracked_file.startswith('.nox/build-release/') + for untracked_file in get_git_untracked_files() ) - if has_git_untracked_files: + if has_forbidden_git_untracked_files: session.error( "There are untracked files in the working directory. " "Remove them and try again",