Commits

Konstantine Rybnikov committed d302a63

add some tests and basic functionality

Comments (0)

Files changed (9)

 syntax: glob
 *.pyc
+#*#
+.#*
 
     >>> from hgfilemerge import HgFileMerge
     >>>    
     >>> merger = HgFileMerge()
-    >>> obj_config = get_yml_config('merge_config.json')
+    >>> obj_config = get_yml_config('merge_config.yml')
     >>> merger.configure_from_obj(obj_config)
-    >>> merger.merge()
+    >>> merger.save()
 
 Also::
 
-    $ cat merge_config.json
-    [
-    {'name': 'merged_most_popular.css',
-     'files': ['1.css', '2.css', '3.css']},
-    {'name': 'merged_site.css',
-     'files': ['4.css', '5.css', '6.css']},
-    ]
+    $ cat merge_config.yml
 # -*- coding: utf-8 -*-
 
-base_path = 'path/to/css'
-groups = [
-    {'name': 'merged_most_popular.css',
-     'files': ['1.css', '2.css', '3.css']},
-    {'name': 'merged_site.css',
-     'files': ['4.css', '5.css', '6.css']},
-    ]
+import os
+import logging
+
+log = logging.getLogger(__name__)
+
+class HgFileMerge():
+    
+    def __init__(self):
+        self.config = None
+        
+    def configure_from_dict(self, d):
+        if not isinstance(self.config, dict):
+            self.config = {}
+        if 'base_path' in d:
+            self.config['base_path'] = d['base_path']
+        if 'merge_groups' in d:
+            if not isinstance(self.config.get('merge_groups'), list):
+                self.config['merge_groups'] = []
+            for group in d['merge_groups']:
+                valid_group = {}
+                valid_group['name'] = group['name']
+                valid_group['files'] = []
+                for file_ in group['files']:
+                    file_path = os.path.join(self.config['base_path'], file_)
+                    if not os.path.exists(file_path):
+                        log.warning(u'File %s does not exist' % file_path)
+                    elif not os.path.isfile(file_path):
+                        log.warning(u'File %s is not a file (directory maybe?)' % file_path)
+                    else:
+                        valid_group['files'].append(file_)
+                self.config['merge_groups'].append(valid_group)
+    
+    def configure_from_file(self, path):
+        import codecs
+        
+        def yml_configurer(f):
+            from yaml import load
+            return load(f)
+        
+        configurers = {
+            'yml': yml_configurer,
+            }
+        
+        configurer = (configurers.get(path[path.rfind('.'):]) or
+                      configurers.get('yml'))
+        
+        with codecs.open(path, 'r', 'utf-8') as f:
+            self.configure_from_dict(configurer(f))
+        
+    def get_merge_groups(self):
+        pass
+
+    

tests/test_main.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+from hgfilemerge import HgFileMerge
+
+class TestYmlConfigParser(unittest.TestCase):
+
+    def test_simple_yml_config(self):
+        merger = HgFileMerge()
+        merger.configure_from_file('tests/testfiles/conf.yml')
+        
+        self.assertEqual(merger.config,
+                         {'base_path': 'tests/testfiles/test_yml_config_parser/',
+                          'merge_groups':
+                              [{'name': 'merged_most_popular.css',
+                                'files':
+                                    ['1.css', '2.css',]},
+                               {'name': 'merged_site.css',
+                                'files':
+                                    ['3.css',]}]})
+
+class TestSimple(unittest.TestCase):
+
+    def setUp(self):
+        import codecs
+        
+        self.merger = HgFileMerge()
+        self.merger.configure_from_file('tests/testfiles/conf.yml')
+        
+        self.f = codecs.open('tests/testfiles/test_yml_config_parser/1_2_merged.css', 'r', 'utf-8')
+        
+    def tearDown(self):
+        self.f.close()
+    
+    def test_merge_groups(self):
+        self.assertEqual(self.merger.get_merge_groups(),
+                         ['merged_most_popular.css',
+                          'merged_site.css'])
+    
+    def test_merge(self):
+        self.assertEqual(self.merger.get_merged(),
+                         self.f.readlines())

tests/testfiles/conf.yml

+base_path: tests/testfiles/test_yml_config_parser/
+merge_groups:
+    - name: merged_most_popular.css
+      files:
+      - 1.css
+      - 2.css
+    - name: merged_site.css
+      files:
+      - 3.css

tests/testfiles/test_yml_config_parser/1.css

+a.red {
+    color: #ff0000;
+}

tests/testfiles/test_yml_config_parser/1_2_merged.css

+a.red {
+    color: #ff0000;
+}
+a.green {
+    color: #00ff00;
+}

tests/testfiles/test_yml_config_parser/2.css

+a.green {
+    color: #00ff00;
+}

tests/testfiles/test_yml_config_parser/3.css

+a.blue {
+    color: #0000ff;
+}