Skip to content

Commit

Permalink
InfoCollector: Collect info from project files
Browse files Browse the repository at this point in the history
Adds a module "InfoCollector" to collect information
from project files using the ``InfoExtractor`` classes.

Related to #131
  • Loading branch information
satwikkansal committed Jul 23, 2017
1 parent 3c306db commit cdc38eb
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
45 changes: 45 additions & 0 deletions coala_quickstart/generation/InfoCollector.py
@@ -0,0 +1,45 @@
from coala_quickstart.info_extractors.EditorconfigInfoExtractor import (
EditorconfigInfoExtractor)
from coala_quickstart.info_extractors.PackageJSONInfoExtractor import (
PackageJSONInfoExtractor)
from coala_quickstart.info_extractors.GemfileInfoExtractor import (
GemfileInfoExtractor)


def collect_info(project_dir):
"""
Collects information extracted by various ``InfoExtractor``
classes and returns them as a dictionary.
"""
editorconfig_info = EditorconfigInfoExtractor(
[".editorconfig"], project_dir).extract_information()

package_json_info = PackageJSONInfoExtractor(
["package.json"], project_dir).extract_information()

gemfile_info = GemfileInfoExtractor(
["Gemfile"], project_dir).extract_information()

extracted_info = aggregate_info([
editorconfig_info, package_json_info, gemfile_info])

return extracted_info


def aggregate_info(infoextractors):
"""
Aggregates inforamtion extracted from multiple ``InfoExtractor``
instances to one dictionary.
:param infoextractors: list of values of ``information`` attribute
of different ``InfoExtractor`` instances.
"""
result = {}
for ie in infoextractors:
for fname, extracted_info in ie.items():
for info_name, info_instances in extracted_info.items():
if result.get(info_name):
result[info_name] += info_instances
else:
result[info_name] = info_instances
return result
100 changes: 100 additions & 0 deletions tests/generation/InfoCollectorTest.py
@@ -0,0 +1,100 @@
import os
import unittest

from coala_quickstart.generation.InfoCollector import (
collect_info)
from tests.TestUtilities import generate_files


package_json = """
{
"name": "awesome-packages",
"version": "0.8.0",
"license": "MIT",
"dependencies": {
"coffeelint": "~1",
"ramllint": ">=1.2.2 <1.2.4"
},
"files": ["dist"],
"man" : ["./man/foo.1", "./man/bar.1"]
}
"""

editorconfig = """
root = true
[*]
end_of_line = lf
insert_final_newline = true
[*.{js,py}]
charset = utf-8
trim_trailing_whitespace = true
indent_size = tab
tab_width = 4
[*.py]
indent_style = space
indent_size = 4
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
"""

gemfile = """
source "https://rubygems.org"
gem "puppet-lint", "2.1.1"
gem "rubocop", "0.47.1"
gem "scss_lint", require: false
gem "RedCloth", :require => "redcloth"
gem "omniauth", ">= 0.2.6", :git => "git://github.com/intridea/omniauth.git"
group :assets do
gem 'some-gem', source: "https://gems.example.com"
end
gem "rspec-rails", ">= 2.6.1", :group => [:development, :test]
end
"""


class InfoCollectorTest(unittest.TestCase):

def setUp(self):
self.uut = collect_info
self.test_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"information_collector_testfiles")

def test_collected_info(self):

files_to_create = ["package.json", ".editorconfig", "Gemfile"]
target_file_contents = [package_json, editorconfig, gemfile]

with generate_files(
files_to_create,
target_file_contents,
self.test_dir) as gen_files:

collected_info = self.uut(self.test_dir)

expected_results = [
('TrailingWhitespaceInfo', ['.editorconfig'], 1),
('FinalNewlineInfo', ['.editorconfig'], 1),
('IndentStyleInfo', ['.editorconfig'], 2),
('IndentSizeInfo', ['.editorconfig'], 3),
('LineBreaksInfo', ['.editorconfig'], 1),
('CharsetInfo', ['.editorconfig'], 1),
('ProjectDependencyInfo', ['Gemfile', 'package.json'], 9),
('ManFilesInfo', ['package.json'], 1),
('LicenseUsedInfo', ['package.json'], 1),
('IncludePathsInfo', ['package.json'], 1)]

self.assertEqual(len(collected_info.keys()), len(expected_results))

for iname, isources, icount in expected_results:
self.assertEqual(len(collected_info[iname]), icount)
isources = [os.path.normcase(i) for i in isources]
for info in collected_info[iname]:
self.assertIn(info.source, isources)

0 comments on commit cdc38eb

Please sign in to comment.