Skip to content

Commit 9cd63ea

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 9cd63ea

File tree

12 files changed

+163
-8
lines changed

12 files changed

+163
-8
lines changed

coala_quickstart/generation/FileGlobs.py

Lines changed: 19 additions & 3 deletions
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
@@ -23,12 +24,27 @@ def get_project_files(log_printer, printer, project_dir, non_interactive=False):
2324
"""
2425
file_globs = ["**"]
2526

26-
ignore_globs = None
2727
if os.path.isfile(os.path.join(project_dir, ".gitignore")):
2828
printer.print("The contents of your .gitignore file for the project "
2929
"will be automatically loaded as the files to ignore.",
3030
color="green")
31-
ignore_globs = get_gitignore_glob(project_dir)
31+
globs = get_gitignore_glob(project_dir)
32+
for glob in globs:
33+
ignore_globs += glob
34+
35+
npmignore_dir_list = []
36+
37+
for dir_name, subdir_name, files in os.walk(project_dir):
38+
if(os.path.isfile(os.path.join(dir_name, ".npmignore"))):
39+
npmignore_dir_list += [dir_name]
40+
41+
if(npmignore_dir_list):
42+
printer.print("The contents of your .npmignore file for the project "
43+
"will be automatically loaded as files to ignore.",
44+
color="green")
45+
globs = get_npmignore_glob(project_dir, npmignore_dir_list)
46+
for glob in globs:
47+
ignore_globs += glob
3248

3349
if non_interactive and not ignore_globs:
3450
ignore_globs = []

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: 97 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,101 @@ 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+
"ignore.c",
160+
os.path.join("src", "ignore.c"),
161+
"glob2.py",
162+
"upload.c",
163+
os.path.join("src", "abc.pyc"),
164+
os.path.join("other_folder", "test.html"),
165+
os.path.join("sample_data", "data", "test.css"),
166+
"run.pyc"]
167+
168+
for file in files + ignored_files:
169+
os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
170+
open(file, "w").close()
171+
files += [os.path.join(".gitignore")]
172+
files += [os.path.join("other_folder", ".npmignore")]
173+
files += [os.path.join("sample_data", "data", ".npmignore")]
174+
175+
npmignore_dir_list = [os.path.join(os.getcwd(), "other_folder"),
176+
os.path.join(os.getcwd(), "sample_data", "data")]
177+
178+
globs = list(get_gitignore_glob(os.getcwd()))
179+
globs += list(get_npmignore_glob(os.getcwd(), npmignore_dir_list))
180+
181+
returned_files = collect_files(
182+
[os.path.join(os.getcwd(), "**")],
183+
self.log_printer,
184+
ignored_file_paths=globs)
185+
files = [os.path.normcase(os.path.abspath(file)) for file in files]
186+
ignored_files = [os.path.abspath(file) for file in ignored_files]
187+
self.maxDiff = None
188+
self.assertEqual(sorted(files), sorted(returned_files))
189+
190+
with suppress_stdout():
191+
self.assertEqual(
192+
sorted(get_project_files(
193+
self.log_printer, self.printer, os.getcwd())[0]),
194+
sorted(files))
195+
196+
os.remove(os.path.join("other_folder", ".npmignore"))
197+
os.remove(os.path.join("sample_data", "data", ".npmignore"))
198+
os.remove(".gitignore")
199+
os.chdir(orig_cwd)
200+
105201
def test_get_project_files_ci_mode(self):
106202
orig_cwd = os.getcwd()
107203
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)