Skip to content

Commit

Permalink
FileGlobs.py : Recursively extract .npmignore glob
Browse files Browse the repository at this point in the history
Looks for .npmignore in dirs and subdirs
recursively and extract file globs

Closes coala#109
  • Loading branch information
prachi1210 committed Aug 8, 2017
1 parent 37573da commit 13e3d9c
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 30 deletions.
18 changes: 17 additions & 1 deletion coala_quickstart/generation/FileGlobs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from coalib.parsing.Globbing import glob_escape
from coala_quickstart.generation.Utilities import get_gitignore_glob
from coala_quickstart.generation.Utilities import (
get_gitignore_glob, get_npmignore_glob)
from coala_utils.Question import ask_question
from coala_quickstart.Strings import GLOB_HELP
from coalib.collecting.Collectors import collect_files
Expand Down Expand Up @@ -30,6 +31,21 @@ def get_project_files(log_printer, printer, project_dir, non_interactive=False):
color="green")
ignore_globs = get_gitignore_glob(project_dir)

npmignore_dir_list = []

for dir_name, subdir_name, files in os.walk(project_dir):
if(os.path.isfile(os.path.join(dir_name, ".npmignore"))):
npmignore_dir_list += [dir_name]

if(npmignore_dir_list):
printer.print("The contents of your .npmignore file for the project "
"will be automatically loaded as files to ignore.",
color="green")
if ignore_globs is None:
ignore_globs = get_npmignore_glob(project_dir, npmignore_dir_list)
else:
ignore_globs += get_npmignore_glob(project_dir, npmignore_dir_list)

if non_interactive and not ignore_globs:
ignore_globs = []

Expand Down
30 changes: 26 additions & 4 deletions coala_quickstart/generation/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ def is_glob_exp(line):
return sum(1 for x in results) != 0


def parse_gitignore_line(line):
def parse_ignore_line(line):
"""
Parses the line from ``.gitignore`` and returns a list of globs.
Parses the line from ``.gitignore`` and ``.npmignore``
and returns a list of globs.
:param line: A line from the project's ``.gitignore`` file.
:param line: A line from the project's ``.gitignore`` or ``.npmignore`` file
:return: A list of glob expressions translated to the
syntax used in coala globbing.
"""
Expand Down Expand Up @@ -72,10 +73,31 @@ def get_gitignore_glob(project_dir, filename=".gitignore"):

with open(gitignore, "r") as file:
for line in file:
for glob in parse_gitignore_line(line):
for glob in parse_ignore_line(line):
yield os.path.join(project_dir, glob)


def get_npmignore_glob(project_dir, npmignore_dir_list, filename=".npmignore"):
"""
Generates a list of glob expressions equivalent to the
contents of the user's project's ``.npmignore`` file.
:param project_dir:
The user's project directory.
:param npmignore_dir_list:
A list of directories in project containing .npmignore
:return:
A list generator of glob expressions generated from the
``.npmignore`` file.
"""
for dir_name in npmignore_dir_list:
npmignore = os.path.join(dir_name, filename)
with open(npmignore, "r") as file:
for line in file:
for glob in parse_ignore_line(line):
yield os.path.join(dir_name, glob)


def split_by_language(project_files):
"""
Splits the given files based on language. This ignores unknown extensions.
Expand Down
148 changes: 123 additions & 25 deletions tests/generation/FileGlobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from coala_utils.ContextManagers import (
simulate_console_inputs, suppress_stdout)
from coala_quickstart.generation.FileGlobs import get_project_files
from coala_quickstart.generation.Utilities import get_gitignore_glob
from coala_quickstart.generation.Utilities import (
get_gitignore_glob, get_npmignore_glob)
from coalib.collecting.Collectors import collect_files


Expand All @@ -30,11 +31,16 @@ def test_get_project_files(self):
open(os.path.join("ignore_dir", "src.js"), "w").close()

with suppress_stdout(), simulate_console_inputs("ignore_dir/**"):
res, _ = get_project_files(self.log_printer, self.printer, os.getcwd())
self.assertIn(os.path.normcase(os.path.join(os.getcwd(), "src", "file.c")), res)
self.assertIn(os.path.normcase(os.path.join(os.getcwd(), "root.c")), res)
self.assertNotIn(os.path.normcase(os.path.join(os.getcwd(), "ignore_dir/src.c")), res)
self.assertNotIn(os.path.normcase(os.path.join(os.getcwd(), "ignore_dir/src.js")), res)
res, _ = get_project_files(
self.log_printer, self.printer, os.getcwd())
self.assertIn(os.path.normcase(
os.path.join(os.getcwd(), "src", "file.c")), res)
self.assertIn(os.path.normcase(
os.path.join(os.getcwd(), "root.c")), res)
self.assertNotIn(os.path.normcase(
os.path.join(os.getcwd(), "ignore_dir/src.c")), res)
self.assertNotIn(os.path.normcase(
os.path.join(os.getcwd(), "ignore_dir/src.js")), res)

os.chdir(orig_cwd)

Expand All @@ -57,21 +63,21 @@ def test_get_project_files_gitignore(self):
# End of gitignore""")

files = [os.path.join("src", "main.c"),
os.path.join("src", "main.h"),
os.path.join("src", "lib", "ssl.c"),
os.path.join("src", "tests", "main.c"),
os.path.join("src", "main.py"),
os.path.join("src", "upload.c"),
".coafile"]
os.path.join("src", "main.h"),
os.path.join("src", "lib", "ssl.c"),
os.path.join("src", "tests", "main.c"),
os.path.join("src", "main.py"),
os.path.join("src", "upload.c"),
".coafile"]
ignored_files = [os.path.join("build", "main.c"),
os.path.join("tests", "run.c"),
os.path.join("src", "build", "main.c"),
"ignore.c",
os.path.join("src", "ignore.c"),
"globexp.py",
"upload.c",
os.path.join("src", "main.pyc"),
"run.pyc"]
os.path.join("tests", "run.c"),
os.path.join("src", "build", "main.c"),
"ignore.c",
os.path.join("src", "ignore.c"),
"globexp.py",
"upload.c",
os.path.join("src", "main.pyc"),
"run.pyc"]

for file in files + ignored_files:
os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
Expand All @@ -97,15 +103,14 @@ def test_get_project_files_gitignore(self):
os.remove(".gitignore")
os.chdir(orig_cwd)


def test_get_project_files_ci_mode(self):
orig_cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)) +
os.sep + "file_globs_ci_testfiles")
os.chdir(os.path.dirname(os.path.realpath(__file__)) +
os.sep + "file_globs_ci_testfiles")

with suppress_stdout():
res, _ = get_project_files(self.log_printer, self.printer, os.getcwd()
, True)
res, _ = get_project_files(
self.log_printer, self.printer, os.getcwd(), True)

paths = [
os.path.join(os.getcwd(), "src", "file.c"),
Expand All @@ -118,3 +123,96 @@ def test_get_project_files_ci_mode(self):
self.assertIn(os.path.normcase(path), res)

os.chdir(orig_cwd)

def test_get_project_files_npmignore(self):
orig_cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.makedirs("file_globs_npmignore_testfiles", exist_ok=True)
os.chdir("file_globs_npmignore_testfiles")

with open(".npmignore", "w") as f:
f.write("""
# Start of npmignore
build
ignore.c
/tests
/upload.c
/*.py
*.pyc
__pycache__
# End of npmignore""")

os.makedirs("other_folder", exist_ok=True)
os.chdir("other_folder")
with open('.npmignore', "w") as file:
file.write("""
#Start of npmignore
*.html
#End of npmignore""")
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir("file_globs_npmignore_testfiles")
os.makedirs("sample_data", exist_ok=True)
os.chdir("sample_data")
os.makedirs("data", exist_ok=True)
os.chdir("data")
with open('.npmignore', "w") as file:
file.write("""
#Start of npmignore
*.css
#End of npmignore""")
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir("file_globs_npmignore_testfiles")
files = [os.path.join("src", "main.c"),
os.path.join("src", "main.h"),
os.path.join("src", "lib", "ssl.c"),
os.path.join("src", "tests", "main.c"),
os.path.join("src", "abc.py"),
os.path.join("src", "upload.c"),
os.path.join("other_folder", "new_file.c"),
os.path.join("sample_data", "data", "new_script.js"),
os.path.join("sample_data", "example.py"),
".coafile"]
ignored_files = [os.path.join("build", "main.c"),
os.path.join("tests", "run.c"),
os.path.join("src", "build", "main.c"),
"ignore.c",
os.path.join("src", "ignore.c"),
"glob2.py",
"upload.c",
os.path.join("src", "abc.pyc"),
os.path.join("other_folder","test.html"),
os.path.join("sample_data","data","test.css"),
"run.pyc"]

for file in files + ignored_files:
os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True)
open(file, "w").close()
files += [".npmignore"]
files += [os.path.join("other_folder", ".npmignore")]
files += [os.path.join("sample_data", "data", ".npmignore")]

npmignore_dir_list = [os.getcwd(),
os.path.join(os.getcwd(), "other_folder"),
os.path.join(os.getcwd(), "sample_data", "data")]

globs = list(get_npmignore_glob(os.getcwd(),npmignore_dir_list))

returned_files = collect_files(
[os.path.join(os.getcwd(), "**")],
self.log_printer,
ignored_file_paths=globs)
files = [os.path.normcase(os.path.abspath(file)) for file in files]
ignored_files = [os.path.abspath(file) for file in ignored_files]
self.maxDiff = None
self.assertEqual(sorted(files), sorted(returned_files))

with suppress_stdout():
self.assertEqual(
sorted(get_project_files(
self.log_printer, self.printer, os.getcwd())[0]),
sorted(files))

os.remove(os.path.join("other_folder",".npmignore"))
os.remove(os.path.join("sample_data", "data", ".npmignore"))
os.remove(".npmignore")
os.chdir(orig_cwd)
Empty file.
10 changes: 10 additions & 0 deletions tests/generation/file_globs_npmignore_testfiles/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# Start of npmignore
build
ignore.c
/tests
/upload.c
/*.py
*.pyc
__pycache__
# End of npmignore
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#Start of npmignore
*.html
#End of npmignore
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#Start of npmignore
*.css
#End of npmignore
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 13e3d9c

Please sign in to comment.