Exemplo n.º 1
0
def test_generate_site():

    # Both try/except blocks below could be a setup method
    # Delete the output folder and create an empty new one
    # This must be done independently
    try:
        shutil.rmtree('output')
    except:
        pass
    try:
        os.mkdir('output')
    except:
        pass

    source_folder = 'test/source'
    output_folder = 'output'
    generate_site(source_folder, output_folder)

    # We know there are only 2 files so it's easier to check with 2 if-s
    # Compare the first output with the first ref
    equal = cmp('output/contact.html', 'test/expected_output/contact.html')
    assert equal == True

    # Compare the first output with the first ref
    equal = cmp('output/index.html', 'test/expected_output/index.html')
    assert equal == True
Exemplo n.º 2
0
    def testGenerateSite(self):
        tmp_dir = None
        try:
            tmp_dir = "".join(random.choice(string.ascii_lowercase + string.digits)
                                  for x in range(40))
            layout_dir = os.path.join(tmp_dir, "layout")
            os.makedirs(tmp_dir)
            os.makedirs(layout_dir)

            # generate temporary source files
            with open(os.path.join(tmp_dir, "index.rst"), "w+") as index_rst:
                index_rst.write(INDEX_RST)
            with open(os.path.join(layout_dir, "home.html"), "w+") as home_html:
                home_html.write(HOME_HTML)

            generate.generate_site(tmp_dir, tmp_dir)

            # test to see if generated content is expected
            with open(os.path.join(tmp_dir, "index.html"), "r") as index_html:
                content = index_html.read().strip()
                self.assertEqual(content, "<h1>My awesome site</h1>")
        finally:
            # delete temporary directories
            if tmp_dir:
                shutil.rmtree(tmp_dir)
Exemplo n.º 3
0
    def testGenerateSite(self):
        tmp_dir = None
        try:
            tmp_dir = "".join(
                random.choice(string.ascii_lowercase + string.digits)
                for x in range(40))
            layout_dir = os.path.join(tmp_dir, "layout")
            os.makedirs(tmp_dir)
            os.makedirs(layout_dir)

            # generate temporary source files
            with open(os.path.join(tmp_dir, "index.rst"), "w+") as index_rst:
                index_rst.write(INDEX_RST)
            with open(os.path.join(layout_dir, "home.html"),
                      "w+") as home_html:
                home_html.write(HOME_HTML)

            generate.generate_site(tmp_dir, tmp_dir)

            # test to see if generated content is expected
            with open(os.path.join(tmp_dir, "index.html"), "r") as index_html:
                content = index_html.read().strip()
                self.assertEqual(content, "<h1>My awesome site</h1>")
        finally:
            # delete temporary directories
            if tmp_dir:
                shutil.rmtree(tmp_dir)
 def test_generate_site_correct_params(self):
     generate.generate_site('test/source', 'output')
     with open('test/expected_output/contact.html') as ec, \
             open('test/expected_output/index.html') as ei, \
             open('output/contact.html') as c, \
             open('output/index.html') as i:
         self.assertEqual(ec.read() + ei.read(), c.read() + i.read())
Exemplo n.º 5
0
 def test_generate_site_correct_params(self):
     generate.generate_site('test/source', 'output')
     with open('test/expected_output/contact.html') as ec, \
             open('test/expected_output/index.html') as ei, \
             open('output/contact.html') as c, \
             open('output/index.html') as i:
         self.assertEqual(ec.read() + ei.read(), c.read() + i.read())
Exemplo n.º 6
0
def test_generate_site_wrong_output():
    input_folder = "test/source"
    output_folder = ""

    with pytest.raises(FileNotFoundError):
        generate.generate_site(input_folder, output_folder)

    assert True
 def test_generate_site_wrong_template_name(self):
     file_wrong_template = get_random_str() + '.rst'
     with open(os.path.join(self.input_path, file_wrong_template), 'w') as f:
         f.write("""{"title": "Test", "layout": "wrong.html"}
                 ---
                 something something""")
     generate.generate_site(self.input_path, self.output_path)
     self.assertFalse(os.path.exists(self.output_path))
 def test_generate_site_no_layout_in_metadata(self):
     file_without_layout = get_random_str() + '.rst'
     with open(os.path.join(self.input_path, file_without_layout), 'w') as f:
         f.write("""{"title": "Test"}
                 ---
                 something something""")
     generate.generate_site(self.input_path, self.output_path)
     self.assertFalse(os.path.exists(self.output_path))
Exemplo n.º 9
0
def test_generate_site_existing_output():
    input_folder = "test/source"
    output_folder = "generate.py"

    with pytest.raises(FileExistsError):
        generate.generate_site(input_folder, output_folder)

    assert True
Exemplo n.º 10
0
 def test_generate_site_wrong_template_name(self):
     file_wrong_template = get_random_str() + '.rst'
     with open(os.path.join(self.input_path, file_wrong_template),
               'w') as f:
         f.write("""{"title": "Test", "layout": "wrong.html"}
                 ---
                 something something""")
     generate.generate_site(self.input_path, self.output_path)
     self.assertFalse(os.path.exists(self.output_path))
Exemplo n.º 11
0
 def test_generate_site_no_layout_in_metadata(self):
     file_without_layout = get_random_str() + '.rst'
     with open(os.path.join(self.input_path, file_without_layout),
               'w') as f:
         f.write("""{"title": "Test"}
                 ---
                 something something""")
     generate.generate_site(self.input_path, self.output_path)
     self.assertFalse(os.path.exists(self.output_path))
Exemplo n.º 12
0
 def test_generate_site_empty(self, mock_call):
     name = "empty.rst"
     with open(os.path.join(self.input, name), 'w') as f:
         f.write("{ }")
     generate.generate_site(self.input, self.output)
     self.assertFalse(
         mock_call.called,
         "Test fail: write_output() is called even if the metadata in rst file is empty"
     )
Exemplo n.º 13
0
    def test_generate_site(self):
        generate.generate_site('test/source/', 'output')

        self.assertTrue(
            filecmp.cmp('test/expected_output2/contact.html',
                        'output/contact.html'))
        self.assertTrue(
            filecmp.cmp('test/expected_output2/index.html',
                        'output/index.html'))
Exemplo n.º 14
0
    def test_generate_site_no_layout_in_metadata(self, mock_call):
        name = "empty_template.rst"
        with open(os.path.join(self.input, name), 'w') as f:
            f.write('''{"title": "Test"}
					---
					content''')
        generate.generate_site(self.input, self.output)
        self.assertFalse(
            mock_call.called,
            "Test fail: write_output() is called even if there is no template")
Exemplo n.º 15
0
    def test_generate_site_invalid_template(self, mock_call):
        name = "invalid_template.rst"
        with open(os.path.join(self.input, name), 'w') as f:
            f.write('''{"title": "title", "layout": "invalid"}
				---
				content''')
        generate.generate_site(self.input, self.output)
        self.assertFalse(
            mock_call.called,
            "Test fail: write_output() is called even if the template is invalid"
        )
Exemplo n.º 16
0
    def _test_generate_site(self, name):
        path = os.path.join('test', 'expected_output', name+'.html')
        expected = open(path).read()
        expected_path = os.path.join(self.OUT_DIR, name+'.html')

        old = generate.list_files
        generate.list_files = lambda p: [os.path.join(self.SRC_DIR, name+'.rst')]
        generate.generate_site(self.SRC_DIR, self.OUT_DIR)
        generate.list_files = old

        return expected_path, expected
Exemplo n.º 17
0
def test_generate_site_no_input():
    input_folder = ""
    output_folder = "test/output"

    with pytest.raises(FileNotFoundError):
        generate.generate_site(input_folder, output_folder)

    # CLEANUP
    shutil.rmtree(output_folder)

    assert True
Exemplo n.º 18
0
def test_generate_site():
    input_folder = "test/source"
    output_folder = "test/output"
    expected_output_folder = "test/expected_output"

    generate.generate_site(input_folder, output_folder)

    result = compare_all_files(output_folder, expected_output_folder)

    # CLEANUP
    shutil.rmtree(output_folder)

    assert result
Exemplo n.º 19
0
def synchronize(db, confirmation=False):
  """
  Fonction en charge de réaliser la synchronisation FTP (et plus tard SSH) avec le serveur distant.
  Effectue effectivement la synchronisation avec le FTP dont les paramètres se trouvent dans conf.
  Lève une exception SyncError en cas de problème.
  @type confirmation: bool
  @param confirmation: confirmation d'ecrasement des fichiers locaux en cas de conflit
  """
  print "<ul><li>"+_("Génération dut site")+"</li>"
  generate.generate_site(db)
  last_cwd = os.getcwd()
  try:
    if conf_private.transfer_mode == 'SSH':
      trans = _transferer_SSH()
    else:
      print "<li>"+_("Connection au serveur FTP")+"</li>"
      trans = _transferer_FTP()
    try:
      os.chdir(confstatic.generate_path)
      find = _finder(trans)
      files_to_download = find.getNewFilesToDownload()
      if find.get_conflict() and not confirmation:
        raise SyncError(_("Conflit entre les fichiers locaux et distants."), True)
      print "<li>"+_("Récupération des fichiers distants plus récents")+"</li><ul>"
      for f in files_to_download:
        trans.retFile(f)
      print "</ul><li>"+_("Envoi des fichiers locaux")+"</li><ul>"
      for f in find.getNewFiles():
        trans.sendFile(f)
      print "</ul>"
      find.save()
      print "</ul>"
    except SyncError, e:
      trans.quit()
      raise e
    trans.quit()
Exemplo n.º 20
0
    def test_generate_site_from_example(self):
        source_dir = "test/source"
        expected_dir = "test/expected_output"
        output_dir = "output"
        result = generate.generate_site(source_dir, output_dir)

        # for each file which we expect
        for f in os.listdir(expected_dir):
            outputted_file = os.path.join(output_dir, f)
            # make sure that the file actually got created
            self.assertTrue(os.path.isfile(outputted_file),
                            "File " + f + " did not get created!")
            # make sure that the file contents are identical
            self.assertTrue(
                filecmp.cmp(outputted_file, os.path.join(expected_dir, f)),
                "Test fail: Unexpected output in " + f)