configurationengine/source/plugins/common/ConeContentPlugin/contentplugin/tests/unittest_content_copy_empty_dirs.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 import os, shutil
       
    18 import unittest
       
    19 
       
    20 from cone.public import plugin, api
       
    21 from testautomation.base_testcase import BaseTestCase
       
    22 from testautomation.unzip_file import unzip_file
       
    23 
       
    24 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    25 TESTDATA_DIR  = os.path.join(ROOT_PATH, 'testdata')
       
    26 TEMP_DIR = os.path.join(ROOT_PATH, 'temp/emptydircopy')
       
    27 
       
    28 class TestContentCopyEmptyDirs(BaseTestCase):
       
    29     
       
    30     def _get_project_and_config(self, workdir, storage_type):
       
    31         # Create the working directory for the test
       
    32         self.remove_if_exists(workdir)
       
    33         os.makedirs(workdir)
       
    34         
       
    35         # Unpack or copy the project into the working directory
       
    36         project_source_zip = os.path.join(TESTDATA_DIR, 'emptydircopy/project.zip')
       
    37         if storage_type == 'fs':
       
    38             project_location = os.path.join(workdir, 'project')
       
    39             unzip_file(project_source_zip, project_location)
       
    40         elif storage_type == 'zs':
       
    41             project_location = os.path.join(workdir, 'project.zip')
       
    42             shutil.copy(project_source_zip, project_location)
       
    43         else:
       
    44             raise ValueError('Invalid storage type %r' % storage_type)
       
    45         
       
    46         # Copy the external content directory
       
    47         unzip_file(os.path.join(TESTDATA_DIR, 'emptydircopy/external_content.zip'),
       
    48                    os.path.join(workdir, 'external_content'))
       
    49         
       
    50         project = api.Project(api.Storage.open(project_location, 'r'))
       
    51         config = project.get_configuration('root.confml')
       
    52         return project, config
       
    53     
       
    54     def test_get_copy_list(self):
       
    55         workdir = os.path.join(TEMP_DIR, 'get_copy_list')
       
    56         proj, conf = self._get_project_and_config(workdir, 'fs')
       
    57         proj.close()
       
    58         impls = plugin.get_impl_set(conf)
       
    59         
       
    60         orig_dir = os.getcwd()
       
    61         os.chdir(workdir)
       
    62         try:
       
    63             self.assertEquals(1, len(impls))
       
    64             impl = iter(impls).next()
       
    65             self.assertEquals(8, len(impl.impls))
       
    66             
       
    67             # Normal inputs
       
    68             # -------------
       
    69             def check(impl_index, expected):
       
    70                 self.assertEquals(sorted(impl.impls[impl_index].get_full_copy_list()),
       
    71                                   sorted(expected))
       
    72             check(0, [
       
    73                 ('layer2/content/foobar/layer2_emptydir',       'foobar_out/layer2_emptydir', False),
       
    74                 ('layer1/content/foobar/layer1_emptydir',       'foobar_out/layer1_emptydir', False),
       
    75                 ('layer1/content/foobar/layer1_emptydir2/foo',  'foobar_out/layer1_emptydir2/foo', False),
       
    76                 ('layer2/content/foobar/layer2.txt',            'foobar_out/layer2.txt', False),
       
    77                 ('layer1/content/foobar/layer1.txt',            'foobar_out/layer1.txt', False)
       
    78             ])
       
    79             check(1, [
       
    80                 #('layer2/content/foobar_filtered/layer2_filtered_emptydir', 'foobar_out/layer2_filtered_emptydir', False),
       
    81                 #('layer1/content/foobar_filtered/layer1_filtered_emptydir', 'foobar_out/layer1_filtered_emptydir', False),
       
    82                 ('layer1/content/foobar_filtered/bar.txt', 'foobar_out_filtered/bar.txt', False),
       
    83             ])
       
    84             check(2, [('layer1/content/empty', 'empty_out', False)])
       
    85             check(3, [])
       
    86             
       
    87             
       
    88             # External inputs
       
    89             # ---------------
       
    90             def check(impl_index, expected):
       
    91                 expected = [(os.path.abspath(src).replace('\\', '/'), tgt, ext) for src, tgt, ext in expected]
       
    92                 self.assertEquals(sorted(impl.impls[impl_index].get_full_copy_list()),
       
    93                                   sorted(expected))
       
    94             check(4, [
       
    95                 ('external_content/foobar/emptydir',       'ext_out/foobar_out/emptydir', True),
       
    96                 ('external_content/foobar/emptydir2/foo',  'ext_out/foobar_out/emptydir2/foo', True),
       
    97                 ('external_content/foobar/x.txt',          'ext_out/foobar_out/x.txt', True)
       
    98             ])
       
    99             check(5, [
       
   100                 #('external_content/foobar_filtered/layer1_filtered_emptydir', 'ext_out/foobar_out/layer1_filtered_emptydir', False),
       
   101                 ('external_content/foobar_filtered/bar.txt', 'ext_out/foobar_out_filtered/bar.txt', True),
       
   102             ])
       
   103             check(6, [('external_content/empty', 'ext_out/empty_out', True)])
       
   104             check(7, [])
       
   105         finally:
       
   106             os.chdir(orig_dir)
       
   107             self.remove_if_exists(workdir)
       
   108     
       
   109     def test_copy_empty_dirs_filestorage(self):
       
   110         workdir = os.path.join(TEMP_DIR, 'filestorage')
       
   111         proj, conf = self._get_project_and_config(workdir, 'fs')
       
   112         
       
   113         orig_dir = os.getcwd()
       
   114         os.chdir(workdir)
       
   115         try:
       
   116             self._run_test_copy_empty_dirs(workdir, conf)
       
   117         finally:
       
   118             os.chdir(orig_dir)
       
   119             proj.close()
       
   120             self.remove_if_exists(workdir)
       
   121     
       
   122     def test_copy_empty_dirs_zipstorage(self):
       
   123         workdir = os.path.join(TEMP_DIR, 'zipstorage')
       
   124         proj, conf = self._get_project_and_config(workdir, 'zs')
       
   125         
       
   126         orig_dir = os.getcwd()
       
   127         os.chdir(workdir)
       
   128         try:
       
   129             self._run_test_copy_empty_dirs(workdir, conf)
       
   130         finally:
       
   131             os.chdir(orig_dir)
       
   132             proj.close()
       
   133             self.remove_if_exists(workdir)
       
   134     
       
   135     def _run_test_copy_empty_dirs(self, workdir, config):
       
   136         output_dir = os.path.join(workdir, 'output')
       
   137         context = plugin.GenerationContext(configuration=config, output=output_dir)
       
   138         impl_set = plugin.get_impl_set(config)
       
   139         impl_set.generate(context)
       
   140         
       
   141         created_dirs = []
       
   142         created_files = []
       
   143         def strip(path):
       
   144             return path[len(output_dir):].replace('\\', '/').strip('/')
       
   145         for root, dirs, files in os.walk(output_dir):
       
   146             for d in dirs:  created_dirs.append(strip(os.path.join(root, d)))
       
   147             for f in files: created_files.append(strip(os.path.join(root, f)))
       
   148         
       
   149         self.assertEquals(sorted(created_dirs), sorted(
       
   150             ['empty_out',
       
   151              'ext_out',
       
   152              'ext_out/empty_out',
       
   153              'ext_out/foobar_out',
       
   154              'ext_out/foobar_out/emptydir',
       
   155              'ext_out/foobar_out/emptydir2',
       
   156              'ext_out/foobar_out/emptydir2/foo',
       
   157              'ext_out/foobar_out_filtered',
       
   158              'foobar_out',
       
   159              'foobar_out/layer1_emptydir',
       
   160              'foobar_out/layer1_emptydir2',
       
   161              'foobar_out/layer1_emptydir2/foo',
       
   162              'foobar_out/layer2_emptydir',
       
   163              'foobar_out_filtered'
       
   164              #'foobar_out_filtered/layer1_emptydir',
       
   165              #'foobar_out_filtered/layer2_emptydir',
       
   166              ]))
       
   167         self.assertEquals(sorted(created_files), sorted(
       
   168             ['ext_out/foobar_out/x.txt',
       
   169              'ext_out/foobar_out_filtered/bar.txt',
       
   170              'foobar_out/layer1.txt',
       
   171              'foobar_out/layer2.txt',
       
   172              'foobar_out_filtered/bar.txt']))
       
   173 
       
   174 if __name__ == '__main__':
       
   175     unittest.main()