Commits

Tod Gentille  committed fcf2896 Merge

Merged in abingham/pluralsight-module-checker (pull request #1)

Updated setup.py to refer to correct readme.md; made some tests work.

  • Participants
  • Parent commits 3e16f96, 8f03e3d

Comments (0)

Files changed (6)

File psmodulecheck/filechecker.py

 """Check that all expected file names exist in module folder and that that they
 conform to the expected names."""
 import os
-from console import (print_error, print_pass)
-import metachecker
-import questions
+
+from psmodulecheck.console import (print_error, print_pass)
+from psmodulecheck import metachecker, questions
 
 
 class FileChecker:
         for file in self._config.question_files:
             if os.path.exists(file):
                 print_pass("Question file exists.")
-                if file.startswith("no-"):
+                if os.path.split(file)[1].startswith("no-"):
                     return True
                 return self.check_question_contents()
 

File psmodulecheck/metachecker.py

 from xml.dom import minidom
 import os
-from console import (print_pass, print_error)
+
+from psmodulecheck.console import (print_pass, print_error)
 
 
 def check_meta_contents(meta_file_name, author_name):

File psmodulecheck/questions.py

 
 import os
 
-from console import (print_error, print_pass)
+from psmodulecheck.console import (print_error, print_pass)
 
 
 class Question:

File psmodulecheck/settings.py

 class Settings:
     SETTINGS_FILE = 'settings.json'
 
-    def __init__(self):
+    def __init__(self, data):
         """
         Initialize all the members except _lastFullPath which is set after the
         user provides the module number.
         """
 
-        data = self._read_settings()
         self.create_full_course = data["createFullCourse"]
         self._partial_path = data['path']
         self.meta_suffix = data['metaSuffix']
         self.module_link_start = data["moduleLinkStart"]
         self.question_file_comment = data["questionFileComment"]
 
-    def _read_settings(self):
-        """Open the json file and read it in and return the contents"""
-        root = os.path.dirname(__file__)
-        settings_path = os.path.join(root, self.SETTINGS_FILE)
-
-        with open(settings_path) as json_file:
-            return json.load(json_file)
-
     def user_set_full_path(self, user_suffix):
         """set _lastFullPath to the full pathname to the module folder
         Args:
     def get_meta_filename(self):
         """Construct the name expected for the meta file from the folder path
         and suffix (typically .meta) specified in the settings file"""
-        return os.path.basename(self.last_full_path) + self.meta_suffix
+        return os.path.join(
+            self._partial_path,
+            os.path.basename(self.last_full_path) + self.meta_suffix)
 
     def get_new_meta_filename(self):
         return os.path.basename(self.full_course_id + self.module_suffix + self.meta_suffix)
 
     def get_question_filename(self):
         index = self.valid_question_file_index
-        return self.question_files[index]
+        return self.question_files[index]
+
+def create_from_file(filename):
+    with open(filename, 'rt') as json_file:
+        return create_from_dict(json.load(json_file))
+
+def create_from_dict(data):
+    return Settings(data)

File psmodulecheck/tests/test_filechecker.py

-import unittest
-import settings
-import filechecker
 import os
 import sys
-import console
+import tempfile
+import unittest
+
+from psmodulecheck import (console,
+                           filechecker,
+                           settings)
+
+
+class TempProject:
+    """Creates a configuration for a temporary PS project, and manages
+    the creation of files in that directory if requested.
+
+    """
+    def __init__(self, root):
+        """Create a new temporary project at ROOT.
+        """
+        self.path = root
+
+        config_dict = {
+            "path": self.path,
+            "shortId": "qt-intro",
+            "courseId": "introduction-qt-cplusplus-framework",
+            "author": "tod-gentille",
+            "createFullCourse": True,
+            "fullCourseDestinationPrefix": "E:\\DropBox\\tod-gentille-test",
+            "metaSuffix": ".meta",
+            "modulePrefix": "-m",
+            "maxQuestionLength": 150,
+            "maxAnswerLength": 100,
+            "demoFiles": [os.path.join(self.path, f)
+                          for f in ("no-demos.txt", "demos.zip")],
+            "questionFiles": [os.path.join(self.path, f)
+                              for f in ("no-questions.txt", "questions.txt")],
+            "validQuestionFileIndex": 1,
+            "questionStart": "Q)",
+            "answerStart": "*",
+            "discriminatorStart": "-",
+            "moduleLinkStart": "=",
+            "questionFileComment": "#"
+        }
+
+        self._config = settings.create_from_dict(config_dict)
+
+    @property
+    def config(self):
+        """The ``Settings`` associated with this project."""
+        return self._config
+
+    def create_files(self):
+        """Create the questions, demos, and meta files for the
+        project.
+
+        """
+        for f in (self.config.question_files[0],
+                  self.config.demo_files[0],
+                  self.config.get_meta_filename()):
+            with open(f, 'w+'):
+                pass
 
 
 class FilecheckerTests(unittest.TestCase):
-    def setUp(self):
-        config = settings.Settings()
-        config.user_set_full_path("01")
-        self.fc = filechecker.FileChecker(config)
-        self.meta_filename = config.get_meta_filename()
-
-    def tearDown(self):
-        pass
-
     @classmethod
     def setUpClass(cls):
         if sys.stdout.isatty():  # this is false in PyCharm Python console
-            console.init()       # doing init from there causes colors to NOT work
+            console.init()       # doing init from there causes colors
+                                 # to NOT work
+        cls.tempdir = tempfile.TemporaryDirectory()
+        proj = TempProject(cls.tempdir.name)
+        proj.config.user_set_full_path("01")
+        proj.create_files()
+        cls.fc = filechecker.FileChecker(proj.config)
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.tempdir.cleanup()
 
     def test_check_demo_files(self):
-        test_file = 'no-demos.txt'
-        open(test_file, 'w+').close()
         actual = self.fc.check_demo_files()
         self.assertTrue(actual)
-        os.remove(test_file)
+
+    def test_check_question_files(self):
+        actual = self.fc.check_question_files()
+        self.assertTrue(actual)
+
+    def test_validate_meta(self):
+        actual = self.fc.check_meta_file()
+        self.assertTrue(actual)
+
+
+class FileCheckerTestsNoFiles(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        if sys.stdout.isatty():  # this is false in PyCharm Python console
+            console.init()       # doing init from there causes colors
+                                 # to NOT work
+        cls.tempdir = tempfile.TemporaryDirectory()
+        proj = TempProject(cls.tempdir.name)
+        proj.config.user_set_full_path("01")
+        cls.fc = filechecker.FileChecker(proj.config)
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.tempdir.cleanup()
 
     def test_check_demo_files_fails(self):
         actual = self.fc.check_demo_files()
         self.assertFalse(actual)
 
-    def test_check_question_files(self):
-        test_file = 'no-questions.txt'
-        open(test_file, 'w+').close()
-        actual = self.fc.check_question_files()
-        self.assertTrue(actual)
-        os.remove(test_file)
-
     def test_check_question_files_fails(self):
         actual = self.fc.check_question_files()
         self.assertFalse(actual)
         actual = self.fc.check_meta_file()
         self.assertFalse(actual)
 
-    def test_validate_meta(self):
-        test_file = self.meta_filename
-        open(test_file, 'w+').close()
-        actual = self.fc.check_meta_file()
-        self.assertTrue(actual)
-        os.remove(test_file)
-
 
 if __name__ == '__main__':
     unittest.main()
 
 setup(
     name='psmodulecheck',
-    version = '0.1dev',
-    author = 'Tod Gentille',
-    author_email = "info@syncorsystems.com",
-    packages = ['psmodulecheck'],
-	#data_files = [('settings.json', '.\psmodulecheck')]
-    package_data = {'psmodulecheck':['./settings.json']},
+    version='0.1dev',
+    author='Tod Gentille',
+    author_email="info@syncorsystems.com",
+    packages=['psmodulecheck'],
+    #data_files=[('settings.json', '.\psmodulecheck')]
+    package_data={'psmodulecheck': ['./settings.json']},
     license='The MIT License (MIT)',
-    description='Validate a PluralSight module folder. Check that all needed files are present and more.',
-    long_description=open('READ_ME.md').read(),
+    description='Validate a PluralSight module folder. '
+                'Check that all needed files are present and more.',
+    long_description=open('ReadMe.md').read(),
     keywords=['PluralSight', 'module checker', 'module validator'],
-	requires=['termcolor', 'colorama'],
+    requires=['termcolor', 'colorama'],
 )