diff --git a/gemfileparser2/__init__.py b/gemfileparser2/__init__.py index 1846634..8f9eb18 100644 --- a/gemfileparser2/__init__.py +++ b/gemfileparser2/__init__.py @@ -14,6 +14,26 @@ import os import re +TRACE = False + + +def logger_debug(*args): + pass + + +if TRACE: + import logging + import sys + + logger = logging.getLogger(__name__) + logging.basicConfig(stream=sys.stdout) + logger.setLevel(logging.DEBUG) + + def logger_debug(*args): + return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args)) + + logger_debug = print + class Dependency(object): """ @@ -157,10 +177,17 @@ def parse_gemfile(self): # Gemfile contains a call to gemspec gemfiledir = os.path.dirname(self.filepath) gemspec_list = glob.glob(os.path.join(gemfiledir, "*.gemspec")) + + if not gemspec_list: + logger_debug(f"No gemspec files found: {gemspec_list}") + continue + if len(gemspec_list) > 1: - print("Multiple gemspec files found") + logger_debug("Multiple gemspec files found") continue + gemspec_file = gemspec_list[0] + # FIXME: the path is not used self.parse_gemspec(path=os.path.join(gemfiledir, gemspec_file)) elif line.startswith("gem "): diff --git a/tests/README.rst b/tests/README.rst deleted file mode 100644 index d94783e..0000000 --- a/tests/README.rst +++ /dev/null @@ -1,2 +0,0 @@ -Put your Python test modules in this directory. - diff --git a/tests/Gemfile b/tests/data/gemfiles/Gemfile similarity index 100% rename from tests/Gemfile rename to tests/data/gemfiles/Gemfile diff --git a/tests/Gemfile-expected.json b/tests/data/gemfiles/Gemfile-expected.json similarity index 100% rename from tests/Gemfile-expected.json rename to tests/data/gemfiles/Gemfile-expected.json diff --git a/tests/Gemfile_1 b/tests/data/gemfiles/Gemfile_1 similarity index 100% rename from tests/Gemfile_1 rename to tests/data/gemfiles/Gemfile_1 diff --git a/tests/Gemfile_1-expected.json b/tests/data/gemfiles/Gemfile_1-expected.json similarity index 100% rename from tests/Gemfile_1-expected.json rename to tests/data/gemfiles/Gemfile_1-expected.json diff --git a/tests/Gemfile_2 b/tests/data/gemfiles/Gemfile_2 similarity index 100% rename from tests/Gemfile_2 rename to tests/data/gemfiles/Gemfile_2 diff --git a/tests/Gemfile_2-expected.json b/tests/data/gemfiles/Gemfile_2-expected.json similarity index 100% rename from tests/Gemfile_2-expected.json rename to tests/data/gemfiles/Gemfile_2-expected.json diff --git a/tests/Gemfile_3 b/tests/data/gemfiles/Gemfile_3 similarity index 100% rename from tests/Gemfile_3 rename to tests/data/gemfiles/Gemfile_3 diff --git a/tests/Gemfile_3-expected.json b/tests/data/gemfiles/Gemfile_3-expected.json similarity index 100% rename from tests/Gemfile_3-expected.json rename to tests/data/gemfiles/Gemfile_3-expected.json diff --git a/tests/Gemfile_4 b/tests/data/gemfiles/Gemfile_4 similarity index 100% rename from tests/Gemfile_4 rename to tests/data/gemfiles/Gemfile_4 diff --git a/tests/Gemfile_4-expected.json b/tests/data/gemfiles/Gemfile_4-expected.json similarity index 100% rename from tests/Gemfile_4-expected.json rename to tests/data/gemfiles/Gemfile_4-expected.json diff --git a/tests/Gemfile_5 b/tests/data/gemfiles/Gemfile_5 similarity index 100% rename from tests/Gemfile_5 rename to tests/data/gemfiles/Gemfile_5 diff --git a/tests/Gemfile_5-expected.json b/tests/data/gemfiles/Gemfile_5-expected.json similarity index 100% rename from tests/Gemfile_5-expected.json rename to tests/data/gemfiles/Gemfile_5-expected.json diff --git a/tests/address_standardization.gemspec b/tests/data/gemspecs/address_standardization.gemspec similarity index 100% rename from tests/address_standardization.gemspec rename to tests/data/gemspecs/address_standardization.gemspec diff --git a/tests/address_standardization.gemspec-expected.json b/tests/data/gemspecs/address_standardization.gemspec-expected.json similarity index 100% rename from tests/address_standardization.gemspec-expected.json rename to tests/data/gemspecs/address_standardization.gemspec-expected.json diff --git a/tests/arel.gemspec b/tests/data/gemspecs/arel.gemspec similarity index 100% rename from tests/arel.gemspec rename to tests/data/gemspecs/arel.gemspec diff --git a/tests/arel.gemspec-expected.json b/tests/data/gemspecs/arel.gemspec-expected.json similarity index 100% rename from tests/arel.gemspec-expected.json rename to tests/data/gemspecs/arel.gemspec-expected.json diff --git a/tests/data/gemspecs/arel2.gemspec b/tests/data/gemspecs/arel2.gemspec new file mode 100644 index 0000000..54967f6 --- /dev/null +++ b/tests/data/gemspecs/arel2.gemspec @@ -0,0 +1,22 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = arel2 + s.version = "2.0.7.beta.20110429111451" + + s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= + s.authors = ["Aaron Patterson", "Bryan Halmkamp", "Emilio Tagua", "Nick Kallen"] + s.date = %q{2011-04-29} + s.description = %q{Arel is a SQL AST manager for Ruby.} + s.email = ["aaron@tenderlovemaking.com", "bryan@brynary.com", "miloops@gmail.com", "nick@example.org"] + s.extra_rdoc_files = ["History.txt", "MIT-LICENSE.txt", "Manifest.txt", "README.markdown"] + s.files = [".autotest", ".gemtest", "History.txt", "MIT-LICENSE.txt"] + s.homepage = %q{http://github.com/rails/arel} + s.rdoc_options = ["--main", "README.markdown"] + s.require_paths = ["lib"] + s.rubyforge_project = %q{arel} + s.rubygems_version = %q{1.6.1} + s.summary = %q{Arel is a SQL AST manager for Ruby} + s.test_files = ["test/attributes/test_attribute.rb", "test/nodes/test_as.rb"] + +end diff --git a/tests/data/gemspecs/arel2.gemspec-expected.json b/tests/data/gemspecs/arel2.gemspec-expected.json new file mode 100644 index 0000000..0a5ec1a --- /dev/null +++ b/tests/data/gemspecs/arel2.gemspec-expected.json @@ -0,0 +1,8 @@ +{ + "development": [], + "runtime": [], + "dependency": [], + "test": [], + "production": [], + "metrics": [] +} \ No newline at end of file diff --git a/tests/logstash-mixin-ecs_compatibility_support.gemspec b/tests/data/gemspecs/logstash-mixin-ecs_compatibility_support.gemspec similarity index 100% rename from tests/logstash-mixin-ecs_compatibility_support.gemspec rename to tests/data/gemspecs/logstash-mixin-ecs_compatibility_support.gemspec diff --git a/tests/logstash-mixin-ecs_compatibility_support.gemspec-expected.json b/tests/data/gemspecs/logstash-mixin-ecs_compatibility_support.gemspec-expected.json similarity index 100% rename from tests/logstash-mixin-ecs_compatibility_support.gemspec-expected.json rename to tests/data/gemspecs/logstash-mixin-ecs_compatibility_support.gemspec-expected.json diff --git a/tests/sample.gemspec b/tests/data/gemspecs/sample.gemspec similarity index 100% rename from tests/sample.gemspec rename to tests/data/gemspecs/sample.gemspec diff --git a/tests/sample.gemspec-expected.json b/tests/data/gemspecs/sample.gemspec-expected.json similarity index 100% rename from tests/sample.gemspec-expected.json rename to tests/data/gemspecs/sample.gemspec-expected.json diff --git a/tests/test_gemfileparser2.py b/tests/test_gemfileparser2.py index 5ea65c4..93f0f3c 100644 --- a/tests/test_gemfileparser2.py +++ b/tests/test_gemfileparser2.py @@ -3,23 +3,28 @@ # Copyright (c) Balasankar C and others # SPDX-License-Identifier: GPL-3.0-or-later OR MIT +import json +import os + from gemfileparser2 import GemfileParser +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') + def check_gemparser_results(test_file, regen=False): """ - Run GemfileParser.parse on `test_file` and check against a JSON file that - contains expected results with the same name as the `test_file` with a + Run GemfileParser.parse on ``test_file`` and check against a JSON file that + contains expected results with the same name as the ``test_file`` with a "-expected.json" suffix appended. """ - import json - + test_file = os.path.join(TEST_DATA_DIR, test_file) gemparser = GemfileParser(test_file) dependencies = { - group: [dep.to_dict() for dep in deps] for group, deps in gemparser.parse().items() + group: [dep.to_dict() for dep in deps] + for group, deps in gemparser.parse().items() } - expected_file = test_file + "-expected.json" + expected_file = f"{test_file}-expected.json" if regen: with open(expected_file, "w") as o: json.dump(dependencies, o, indent=2) @@ -31,40 +36,44 @@ def check_gemparser_results(test_file, regen=False): def test_source_only_gemfile(): - check_gemparser_results("tests/Gemfile") + check_gemparser_results("gemfiles/Gemfile") def test_gemfile_1(): - check_gemparser_results("tests/Gemfile_1") + check_gemparser_results("gemfiles/Gemfile_1") def test_gemfile_2(): - check_gemparser_results("tests/Gemfile_2") + check_gemparser_results("gemfiles/Gemfile_2") def test_gemfile_3(): - check_gemparser_results("tests/Gemfile_3") + check_gemparser_results("gemfiles/Gemfile_3") def test_gemfile_4(): - check_gemparser_results("tests/Gemfile_4") + check_gemparser_results("gemfiles/Gemfile_4") def test_gemfile_platforms(): - check_gemparser_results("tests/Gemfile_5") + check_gemparser_results("gemfiles/Gemfile_5") def test_gemspec_1(): - check_gemparser_results("tests/sample.gemspec") + check_gemparser_results("gemspecs/sample.gemspec") def test_gemspec_2(): - check_gemparser_results("tests/address_standardization.gemspec") + check_gemparser_results("gemspecs/address_standardization.gemspec") def test_gemspec_3(): - check_gemparser_results("tests/arel.gemspec") + check_gemparser_results("gemspecs/arel.gemspec") + + +def test_gemspec_no_deps(): + check_gemparser_results("gemspecs/arel2.gemspec", regen=False) def test_gemspec_4(): - check_gemparser_results("tests/logstash-mixin-ecs_compatibility_support.gemspec") + check_gemparser_results("gemspecs/logstash-mixin-ecs_compatibility_support.gemspec")