Repository: yevhen-m/flake8-fancy-header

Issue:
Checking filename in header
I think assumption you've made here is incorrect:
https://github.com/yevhen-m/flake8-fancy-header/blob/master/flake8_fancy_header/checker.py#L24

Path python started from have nothing to deal with name of the file it checks. For example, my checker runs from different directory. Can you just check that given path exist on given machine and stop. 
For example you have `fokker.hotels.sell` just check that relpath exist for give file. 
Other solution you can be search for first `setup.cfg` down in the path. 

Discussion:

Patch:
diff --git a/flake8_fancy_header/__init__.py b/flake8_fancy_header/__init__.py
index 4dc17be..d871073 100644
--- a/flake8_fancy_header/__init__.py
+++ b/flake8_fancy_header/__init__.py
@@ -1,9 +1,15 @@
+"""
+===================
+flake8_fancy_header
+===================
+"""
+
 import sys
 
 major, minor = sys.version_info[0], sys.version_info[1]
 PY_37_OR_GREATER = (major, minor) >= (3, 7)
 
 if PY_37_OR_GREATER:
-    from .checker import FancyHeaderChecker
+    from .checker import FancyHeaderChecker  # flake8: noqa
 else:
-    from .checker import FancyHeaderCheckerBefore37 as FancyHeaderChecker
+    from .checker import FancyHeaderCheckerBefore37 as FancyHeaderChecker  # flake8: noqa
diff --git a/flake8_fancy_header/checker.py b/flake8_fancy_header/checker.py
index 2261a33..eaa9baa 100644
--- a/flake8_fancy_header/checker.py
+++ b/flake8_fancy_header/checker.py
@@ -1,10 +1,22 @@
+"""
+===========================
+flake8_fancy_header.checker
+===========================
+"""
+
 __version__ = '0.1.0'
 
 
 import ast
 
 from os import getcwd
-from os.path import join, normpath, splitext
+from os.path import exists, dirname, join, normpath, splitext
+
+PROJECT_ROOT_MARKERS = [
+    '.git',
+    'setup.cfg',
+    'setup.py',
+]
 
 
 class BaseChecker(object):
@@ -18,12 +30,25 @@ class BaseChecker(object):
         self.tree = tree
         self.filename = filename
 
-    def get_header_value(self):
-        # Assume that python runs from project's root, so we can get its
-        # working directory and resolve from it.
-        cwd = getcwd() + '/'
-        filename = normpath(join(cwd, self.filename)).split(cwd, 1)[1]
+    def get_project_root_dir(self, current_dir=None):
+        if current_dir == '/':
+            # Reached fs root, fallback to cwd
+            return getcwd()
+
+        if current_dir is None:
+            current_dir = getcwd()
 
+        for marker in PROJECT_ROOT_MARKERS:
+            if exists(join(current_dir, marker)):
+                return current_dir
+
+        return self.get_project_root_dir(current_dir=dirname(current_dir))
+
+    def get_header_value(self):
+        filename = (
+            normpath(join(getcwd(), self.filename))
+            .split(self.get_project_root_dir() + '/', 1)[1]
+        )
         import_path = splitext(filename)[0].replace('/', '.')
         if import_path.endswith('__init__'):
             import_path = import_path.rsplit('.', 1)[0]
diff --git a/setup.py b/setup.py
index 1c7bc6d..777605d 100644
--- a/setup.py
+++ b/setup.py
@@ -1,3 +1,9 @@
+"""
+=====
+setup
+=====
+"""
+
 import setuptools
 
 install_requires = [


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
index e69de29..31cbc3e 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -0,0 +1,5 @@
+"""
+=====
+tests
+=====
+"""
diff --git a/tests/test_checker.py b/tests/test_checker.py
index d16f912..b229032 100644
--- a/tests/test_checker.py
+++ b/tests/test_checker.py
@@ -1,7 +1,13 @@
+"""
+==================
+tests.test_checker
+==================
+"""
+
 import ast
-import os
 import unittest
 
+from os.path import dirname
 from unittest.mock import patch, Mock
 
 from flake8_fancy_header import FancyHeaderChecker
@@ -11,6 +17,7 @@ from flake8_fancy_header import FancyHeaderChecker
     'flake8_fancy_header.checker.getcwd',
     Mock(return_value='/home/User/Project'),
 )
+@patch('flake8_fancy_header.checker.exists', Mock(return_value=False))
 class CheckerTestCase(unittest.TestCase):
 
     def test_empty_module(self):
@@ -138,3 +145,25 @@ package.spam
             filename='/home/User/Project/package/spam.py',
         )
         self.assertEqual(len(list(checker.run())), 0)
+
+
+class CheckerTestCase1(unittest.TestCase):
+
+    @patch('flake8_fancy_header.checker.exists')
+    @patch('flake8_fancy_header.checker.getcwd')
+    def test_checker_not_from_project_root(self, getcwd, exists):
+        getcwd.return_value = '/home/User/Project/package/subpackage'
+        # Pretend that project root marker was found in /home/User/Project
+        exists.side_effect = lambda path: dirname(path) == '/home/User/Project'
+        module = ast.parse('''\
+"""
+=========================
+package.subpackage.module
+=========================
+"""
+        ''')
+        checker = FancyHeaderChecker(
+            tree=module,
+            filename='module.py',
+        )
+        self.assertEqual(len(list(checker.run())), 0)