Skip to content

Commit b41c6c4

Browse files
committed
FileGlobs.py: Recursively extract .npmignore globs
Looks for .npmignore in dirs and subdirs recursively and extract file globs Closes #109
1 parent 4520477 commit b41c6c4

File tree

12 files changed

+163
-6
lines changed

12 files changed

+163
-6
lines changed

coala_quickstart/generation/FileGlobs.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22

33
from coalib.parsing.Globbing import glob_escape
4-
from coala_quickstart.generation.Utilities import get_gitignore_glob
4+
from coala_quickstart.generation.Utilities import (
5+
get_gitignore_glob, get_npmignore_glob)
56
from coala_utils.Question import ask_question
67
from coala_quickstart.Strings import GLOB_HELP
78
from coalib.collecting.Collectors import collect_files
@@ -30,6 +31,21 @@ def get_project_files(log_printer, printer, project_dir, non_interactive=False):
3031
color="green")
3132
ignore_globs = get_gitignore_glob(project_dir)
3233

34+
npmignore_dir_list = []
35+
36+
for dir_name, subdir_name, files in os.walk(project_dir):
37+
if(os.path.isfile(os.path.join(dir_name, ".npmignore"))):
38+
npmignore_dir_list += [dir_name]
39+
40+
if(npmignore_dir_list):
41+
printer.print("The contents of your .npmignore file for the project "
42+
"will be automatically loaded as files to ignore.",
43+
color="green")
44+
if ignore_globs is None:
45+
ignore_globs = get_npmignore_glob(project_dir, npmignore_dir_list)
46+
else:
47+
ignore_globs += get_npmignore_glob(project_dir, npmignore_dir_list)
48+
3349
if non_interactive and not ignore_globs:
3450
ignore_globs = []
3551

coala_quickstart/generation/Utilities.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ def is_glob_exp(line):
1818
return sum(1 for x in results) != 0
1919

2020

21-
def parse_gitignore_line(line):
21+
def parse_ignore_line(line):
2222
"""
23-
Parses the line from ``.gitignore`` and returns a list of globs.
23+
Parses the line from ``.gitignore`` and ``.npmignore``
24+
and returns a list of globs.
2425
25-
:param line: A line from the project's ``.gitignore`` file.
26+
:param line: A line from the project's ``.gitignore`` or ``.npmignore`` file
2627
:return: A list of glob expressions translated to the
2728
syntax used in coala globbing.
2829
"""
@@ -72,10 +73,31 @@ def get_gitignore_glob(project_dir, filename=".gitignore"):
7273

7374
with open(gitignore, "r") as file:
7475
for line in file:
75-
for glob in parse_gitignore_line(line):
76+
for glob in parse_ignore_line(line):
7677
yield os.path.join(project_dir, glob)
7778

7879

80+
def get_npmignore_glob(project_dir, npmignore_dir_list, filename=".npmignore"):
81+
"""
82+
Generates a list of glob expressions equivalent to the
83+
contents of the user's project's ``.npmignore`` file.
84+
85+
:param project_dir:
86+
The user's project directory.
87+
:param npmignore_dir_list:
88+
A list of directories in project containing .npmignore
89+
:return:
90+
A list generator of glob expressions generated from the
91+
``.npmignore`` file.
92+
"""
93+
for dir_name in npmignore_dir_list:
94+
npmignore = os.path.join(dir_name, filename)
95+
with open(npmignore, "r") as file:
96+
for line in file:
97+
for glob in parse_ignore_line(line):
98+
yield os.path.join(dir_name, glob)
99+
100+
79101
def split_by_language(project_files):
80102
"""
81103
Splits the given files based on language. This ignores unknown extensions.

tests/generation/FileGlobs.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from coala_utils.ContextManagers import (
77
simulate_console_inputs, suppress_stdout)
88
from coala_quickstart.generation.FileGlobs import get_project_files
9-
from coala_quickstart.generation.Utilities import get_gitignore_glob
9+
from coala_quickstart.generation.Utilities import (
10+
get_gitignore_glob, get_npmignore_glob)
1011
from coalib.collecting.Collectors import collect_files
1112

1213

@@ -102,6 +103,103 @@ def test_get_project_files_gitignore(self):
102103
os.remove(".gitignore")
103104
os.chdir(orig_cwd)
104105

106+
def test_get_project_files_npmignore(self):
107+
orig_cwd = os.getcwd()
108+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
109+
os.makedirs("file_globs_npmignore_testfiles", exist_ok=True)
110+
os.chdir("file_globs_npmignore_testfiles")
111+
112+
with open(".gitignore", "w") as f:
113+
f.write("""
114+
# Start of gitignore
115+
buildtest
116+
ignore.c
117+
/testignore
118+
/upload.c
119+
/*.py
120+
*.pyc
121+
__pycache__
122+
# End of gitignore
123+
""")
124+
125+
os.makedirs("other_folder", exist_ok=True)
126+
os.chdir("other_folder")
127+
with open('.npmignore', "w") as file:
128+
file.write("""
129+
#Start of npmignore
130+
*.html
131+
#End of npmignore
132+
""")
133+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
134+
os.chdir("file_globs_npmignore_testfiles")
135+
os.makedirs("sample_data", exist_ok=True)
136+
os.chdir("sample_data")
137+
os.makedirs("data", exist_ok=True)
138+
os.chdir("data")
139+
with open('.npmignore', "w") as file:
140+
file.write("""
141+
#Start of npmignore
142+
*.css
143+
#End of npmignore
144+
""")
145+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
146+
os.chdir("file_globs_npmignore_testfiles")
147+
files = [os.path.join("src", "main.c"),
148+
os.path.join("src", "main.h"),
149+
os.path.join("src", "lib", "ssl.c"),
150+
os.path.join("src", "tests", "main.c"),
151+
os.path.join("src", "abc.py"),
152+
os.path.join("src", "upload.c"),
153+
os.path.join("other_folder", "new_file.c"),
154+
os.path.join("sample_data", "data", "new_script.js"),
155+
os.path.join("sample_data", "example.py"),
156+
".coafile"]
157+
ignored_files = [os.path.join("buildtest", "main.c"),
158+
os.path.join("testignore", "run.c"),
159+
os.path.join("src", "build", "main.c"),
160+
"ignore.c",
161+
os.path.join("src", "ignore.c"),
162+
"glob2.py",
163+
"upload.c",
164+
os.path.join("src", "abc.pyc"),
165+
os.path.join("other_folder", "test.html"),
166+
os.path.join("sample_data", "data", "test.css"),
167+
"run.pyc"]
168+
169+
for file in files + ignored_files:
170+
os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
171+
open(file, "w").close()
172+
files += [".gitignore"]
173+
files += [os.path.join("other_folder", ".npmignore")]
174+
files += [os.path.join("sample_data", "data", ".npmignore")]
175+
176+
gitignore_dir_list = [os.path.join(os.getcwd())]
177+
npmignore_dir_list = [os.path.join(os.getcwd(), "other_folder"),
178+
os.path.join(os.getcwd(), "sample_data", "data")]
179+
180+
globs = list(get_gitignore_glob(os.getcwd(), gitignore_dir_list))
181+
globs += list(get_npmignore_glob(os.getcwd(), npmignore_dir_list))
182+
183+
returned_files = collect_files(
184+
[os.path.join(os.getcwd(), "**")],
185+
self.log_printer,
186+
ignored_file_paths=globs)
187+
files = [os.path.normcase(os.path.abspath(file)) for file in files]
188+
ignored_files = [os.path.abspath(file) for file in ignored_files]
189+
self.maxDiff = None
190+
self.assertEqual(sorted(files), sorted(returned_files))
191+
192+
with suppress_stdout():
193+
self.assertEqual(
194+
sorted(get_project_files(
195+
self.log_printer, self.printer, os.getcwd())[0]),
196+
sorted(files))
197+
198+
os.remove(os.path.join("other_folder", ".npmignore"))
199+
os.remove(os.path.join("sample_data", "data", ".npmignore"))
200+
os.remove(".gitignore")
201+
os.chdir(orig_cwd)
202+
105203
def test_get_project_files_ci_mode(self):
106204
orig_cwd = os.getcwd()
107205
os.chdir(os.path.dirname(os.path.realpath(__file__)) +

tests/generation/file_globs_npmignore_testfiles/.coafile

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# Start of gitignore
3+
buildtest
4+
ignore.c
5+
/testignore
6+
/upload.c
7+
/*.py
8+
*.pyc
9+
__pycache__
10+
# End of gitignore
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#Start of npmignore
3+
*.html
4+
#End of npmignore
5+

tests/generation/file_globs_npmignore_testfiles/other_folder/new_file.c

Whitespace-only changes.

tests/generation/file_globs_npmignore_testfiles/other_folder/test.html

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#Start of npmignore
3+
*.css
4+
#End of npmignore
5+

tests/generation/file_globs_npmignore_testfiles/sample_data/data/new_script.js

Whitespace-only changes.

0 commit comments

Comments
 (0)