Esempio n. 1
0
 def test_external_combine(self):
     # test6: include one external resource but with combine=True, which is unsupported
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     with self.assertRaises(errors.ConfigurationError):
         got = Resources([test],
                         self.tempdir + os.sep + 'rsrc',
                         combineresources=True).make()
Esempio n. 2
0
    def test_combining(self):
        # Test2: combining, resources specified by global config
        # (maybe we should use smaller CSS+JS files? Test takes 2+ seconds...)
        s = os.sep
        want = {'css':[s.join(['rsrc', 'css','combined.css'])],
                'js':[s.join(['rsrc', 'js','combined.js'])],
                'img': [],
                'xml':[s.join(['rsrc', 'resources.xml'])]
        }
        testcss = ["css/ferenda.css",
                   "res/css/fake1.css",
                   "res/css/fake2.css"]
        testjs = ["js/ferenda.js",
                  "res/js/fake1.js",
                  "res/js/fake2.js"]
        resources = Resources([staticmockclass(),staticmockclass2()],self.tempdir+os.sep+'rsrc',
                              combineresources=True,
                              cssfiles=testcss,
                              jsfiles=testjs,
                              sitename="Blahonga",
                              sitedescription="A non-default value")
        rl = resources.resourceloader
        testcssfiles = []
        testjsfiles = []
        for cssfile in testcss:
            try:
                testcssfiles.append(rl.filename(cssfile))
            except errors.ResourceNotFound:
                util.writefile(cssfile, "/* this is a faked css file: %s */" % cssfile*1000)
                testcssfiles.append(cssfile)

        for jsfile in testjs:
            try:
                testjsfiles.append(rl.filename(jsfile))
            except errors.ResourceNotFound:
                util.writefile(jsfile, "/* this is a faked js file: %s */" % jsfile*1000)
                testjsfiles.append(jsfile)

        got = resources.make(api=False)
        self.assertEqual(want,got)
        tree = ET.parse(self.tempdir+'/'+got['xml'][0])
        stylesheets=tree.find("stylesheets").getchildren()
        self.assertEqual(len(stylesheets),1)
        self.assertEqual(stylesheets[0].attrib['href'],'rsrc/css/combined.css')
        javascripts=tree.find("javascripts").getchildren()
        self.assertEqual(len(javascripts),1)
        self.assertEqual(javascripts[0].attrib['src'],'rsrc/js/combined.js')
        self.assertEqual(tree.find("sitename").text,"Blahonga")
        self.assertEqual(tree.find("sitedescription").text,"A non-default value")
        self.assertTrue(os.path.exists(self.tempdir+'/rsrc/css/combined.css'))
        self.assertTrue(os.path.exists(self.tempdir+'/rsrc/js/combined.js'))
        # check that the combining/minifying indeed saved us some space
        self.assertLess(os.path.getsize(self.tempdir+'/rsrc/css/combined.css'),
                        sum([os.path.getsize(x) for x in testcssfiles]))
        self.assertLess(os.path.getsize(self.tempdir+'/rsrc/js/combined.js'),
                        sum([os.path.getsize(x) for x in testjsfiles]))
Esempio n. 3
0
    def test_combining(self):
        # Test2: combining, resources specified by global config
        # (maybe we should use smaller CSS+JS files? Test takes 2+ seconds...)
        s = os.sep
        want = {'css':[s.join(['rsrc', 'css','combined.css'])],
                'js':[s.join(['rsrc', 'js','combined.js'])],
                'img': [],
                'xml':[s.join(['rsrc', 'resources.xml'])]
        }
        testcss = ["css/ferenda.css",
                   "res/css/fake1.css",
                   "res/css/fake2.css"]
        testjs = ["js/ferenda.js",
                  "res/js/fake1.js",
                  "res/js/fake2.js"]
        resources = Resources([staticmockclass(),staticmockclass2()],self.tempdir+os.sep+'rsrc',
                              combineresources=True,
                              cssfiles=testcss,
                              jsfiles=testjs,
                              sitename="Blahonga",
                              sitedescription="A non-default value")
        rl = resources.resourceloader
        testcssfiles = []
        testjsfiles = []
        for cssfile in testcss:
            try:
                testcssfiles.append(rl.filename(cssfile))
            except errors.ResourceNotFound:
                util.writefile(cssfile, "/* this is a faked css file: %s */" % cssfile*1000)
                testcssfiles.append(cssfile)

        for jsfile in testjs:
            try:
                testjsfiles.append(rl.filename(jsfile))
            except errors.ResourceNotFound:
                util.writefile(jsfile, "/* this is a faked js file: %s */" % jsfile*1000)
                testjsfiles.append(jsfile)

        got = resources.make(api=False)
        self.assertEqual(want,got)
        tree = ET.parse(self.tempdir+'/'+got['xml'][0])
        stylesheets=tree.find("stylesheets").getchildren()
        self.assertEqual(len(stylesheets),1)
        self.assertEqual(stylesheets[0].attrib['href'],'rsrc/css/combined.css')
        javascripts=tree.find("javascripts").getchildren()
        self.assertEqual(len(javascripts),1)
        self.assertEqual(javascripts[0].attrib['src'],'rsrc/js/combined.js')
        self.assertEqual(tree.find("sitename").text,"Blahonga")
        self.assertEqual(tree.find("sitedescription").text,"A non-default value")
        self.assertTrue(os.path.exists(self.tempdir+'/rsrc/css/combined.css'))
        self.assertTrue(os.path.exists(self.tempdir+'/rsrc/js/combined.js'))
        # check that the combining/minifying indeed saved us some space
        self.assertLess(os.path.getsize(self.tempdir+'/rsrc/css/combined.css'),
                        sum([os.path.getsize(x) for x in testcssfiles]))
        self.assertLess(os.path.getsize(self.tempdir+'/rsrc/js/combined.js'),
                        sum([os.path.getsize(x) for x in testjsfiles]))
Esempio n. 4
0
 def test_nonexistent_resource(self):
     # test9: nonexistent resources should not be included
     s = os.sep
     test = staticmockclass()        
     test.config.cssfiles = ['nonexistent.css']
     want = {'css':[],
             'img':[s.join(['rsrc', 'img','test.png'])],
             'js':[s.join(['rsrc', 'js','test.js'])],
             'xml':[s.join(['rsrc', 'resources.xml'])]
     }
     with silence():
         got = Resources([test], self.tempdir+os.sep+'rsrc',
                         cssfiles=[],
                         jsfiles=[],
                         imgfiles=[]).make(api=False)
     self.assertEqual(want,got)
Esempio n. 5
0
 def test_external_resource(self):
     # test5: include one external resource, combine=False
     s = os.sep
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     want = {'css':[s.join(['rsrc', 'css','test.css']),
                    'http://example.org/css/main.css'],
             'js':[s.join(['rsrc', 'js','test.js'])],
             'img':[s.join(['rsrc', 'img','test.png'])],
             'xml':[s.join(['rsrc', 'resources.xml'])]
     }
     got = Resources([test],self.tempdir+os.sep+'rsrc',
                     cssfiles=[],
                     jsfiles=[],
                     imgfiles=[]).make(api=False)
     self.assertEqual(want,got)
Esempio n. 6
0
 def test_nonexistent_resource(self):
     # test9: nonexistent resources should not be included
     s = os.sep
     test = staticmockclass()        
     test.config.cssfiles = ['nonexistent.css']
     want = {'css':[],
             'img':[s.join(['rsrc', 'img','test.png'])],
             'js':[s.join(['rsrc', 'js','test.js'])],
             'xml':[s.join(['rsrc', 'resources.xml'])]
     }
     with silence():
         got = Resources([test], self.tempdir+os.sep+'rsrc',
                         cssfiles=[],
                         jsfiles=[],
                         imgfiles=[]).make(api=False)
     self.assertEqual(want,got)
Esempio n. 7
0
 def test_external_resource(self):
     # test5: include one external resource, combine=False
     s = os.sep
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     want = {'css':[s.join(['rsrc', 'css','test.css']),
                    'http://example.org/css/main.css'],
             'js':[s.join(['rsrc', 'js','test.js'])],
             'img':[s.join(['rsrc', 'img','test.png'])],
             'xml':[s.join(['rsrc', 'resources.xml'])]
     }
     got = Resources([test],self.tempdir+os.sep+'rsrc',
                     cssfiles=[],
                     jsfiles=[],
                     imgfiles=[]).make(api=False)
     self.assertEqual(want,got)
Esempio n. 8
0
 def test_basic(self):
     # Test1: No combining, resources specified by docrepos only
     # (skip the default css/js files)
     s = os.sep
     want = {
         'css': [s.join(['rsrc', 'css', 'test.css'])],
         'js': [s.join(['rsrc', 'js', 'test.js'])],
         'img': [s.join(['rsrc', 'img', 'test.png'])],
         'json': [
             s.join(['rsrc', 'api', 'context.json']),
             s.join(['rsrc', 'api', 'common.json']),
             s.join(['rsrc', 'api', 'terms.json'])
         ],
         'xml': [s.join(['rsrc', 'resources.xml'])]
     }
     got = Resources(
         [staticmockclass(), staticmockclass2()],
         self.tempdir + os.sep + 'rsrc',
         cssfiles=[],
         jsfiles=[],
         imgfiles=[]).make()
     self.assertEqual(want, got)
     tree = ET.parse(self.tempdir + os.sep + got['xml'][0])
     stylesheets = tree.find("stylesheets").getchildren()
     self.assertEqual(len(stylesheets), 1)
     self.assertEqual(stylesheets[0].attrib['href'], 'rsrc/css/test.css')
     javascripts = tree.find("javascripts").getchildren()
     self.assertEqual(len(javascripts), 1)
     self.assertEqual(javascripts[0].attrib['src'], 'rsrc/js/test.js')
     # the javascript tag must have some text content to avoid it
     # being self-closed (which doesn't go well with HTML5)
     self.assertTrue(javascripts[0].text)
     self.assertEqual(tree.find("sitename").text, "MySite")
     self.assertEqual(
         tree.find("sitedescription").text, "Just another Ferenda site")
     self.assertEqual(tree.find("url").text, "http://localhost:8000/")
     self.assertTrue(os.path.exists(self.tempdir + '/rsrc/css/test.css'))
     self.assertTrue(os.path.exists(self.tempdir + '/rsrc/js/test.js'))
     tabs = tree.find("tabs")
     self.assertTrue(tabs is not None)
     search = tree.find("search")
     self.assertTrue(search is not None)
Esempio n. 9
0
 def test_windows_paths(self):
     # test8: test win32 path generation on all OS:es, including one full URL
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     want = {'css':['rsrc\\css\\test.css',
                    'http://example.org/css/main.css'],
             'js':['rsrc\\js\\test.js'],
             'img':['rsrc\\img\\test.png'],
             'xml':['rsrc\\resources.xml']}
     try:
         realsep = os.sep
         os.sep = "\\"
         from ferenda import resources
         resources.os.sep = "\\"
         got = Resources([test], self.tempdir+os.sep+'rsrc',
                         cssfiles=[],
                         jsfiles=[],
                         imgfiles=[]).make(api=False)
         self.assertEqual(want,got)
     finally:
         os.sep = realsep
Esempio n. 10
0
 def test_windows_paths(self):
     # test8: test win32 path generation on all OS:es, including one full URL
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     want = {'css':['rsrc\\css\\test.css',
                    'http://example.org/css/main.css'],
             'js':['rsrc\\js\\test.js'],
             'img':['rsrc\\img\\test.png'],
             'xml':['rsrc\\resources.xml']}
     try:
         realsep = os.sep
         os.sep = "\\"
         from ferenda import resources
         resources.os.sep = "\\"
         got = Resources([test], self.tempdir+os.sep+'rsrc',
                         cssfiles=[],
                         jsfiles=[],
                         imgfiles=[]).make(api=False)
         self.assertEqual(want,got)
     finally:
         os.sep = realsep
Esempio n. 11
0
 def test_basic(self):
     # Test1: No combining, resources specified by docrepos only
     # (skip the default css/js files)
     s = os.sep
     want = {'css':[s.join(['rsrc', 'css','test.css'])],
             'js':[s.join(['rsrc', 'js','test.js'])],
             'img':[s.join(['rsrc', 'img','test.png'])],
             'json': [s.join(['rsrc','api','context.json']),
                      s.join(['rsrc','api','common.json']),
                      s.join(['rsrc','api','terms.json'])],
             'xml':[s.join(['rsrc', 'resources.xml'])]
     }
     got = Resources([staticmockclass(),staticmockclass2()],
                     self.tempdir+os.sep+'rsrc',
                     cssfiles=[],
                     jsfiles=[],
                     imgfiles=[]).make()
     self.assertEqual(want, got)
     tree = ET.parse(self.tempdir+os.sep+got['xml'][0])
     stylesheets=tree.find("stylesheets").getchildren()
     self.assertEqual(len(stylesheets),1)
     self.assertEqual(stylesheets[0].attrib['href'],'rsrc/css/test.css')
     javascripts=tree.find("javascripts").getchildren()
     self.assertEqual(len(javascripts),1)
     self.assertEqual(javascripts[0].attrib['src'],'rsrc/js/test.js')
     # the javascript tag must have some text content to avoid it
     # being self-closed (which doesn't go well with HTML5)
     self.assertTrue(javascripts[0].text)
     self.assertEqual(tree.find("sitename").text,"MySite")
     self.assertEqual(tree.find("sitedescription").text,"Just another Ferenda site")
     self.assertEqual(tree.find("url").text,"http://localhost:8000/")
     self.assertTrue(os.path.exists(self.tempdir+'/rsrc/css/test.css'))
     self.assertTrue(os.path.exists(self.tempdir+'/rsrc/js/test.js'))
     tabs=tree.find("tabs")
     self.assertTrue(tabs is not None)
     search=tree.find("search")
     self.assertTrue(search is not None)
Esempio n. 12
0
 def test_external_combine(self):
     # test6: include one external resource but with combine=True, which is unsupported
     test = staticmockclass()
     test.config.cssfiles.append('http://example.org/css/main.css')
     with self.assertRaises(errors.ConfigurationError):
         got = Resources([test],self.tempdir+os.sep+'rsrc', combineresources=True).make()