def setUp(self): self.cwd = os.getcwd() self.local_project_testdir = "build-tests/remote-project/build" self.local_proj_zip_key = "zip_key.yaml" # executed from the same directory # remove all contents of the build directory and build directory path from prior test run if dir_exists('build-tests/remote-project/build'): shutil.rmtree('build-tests/remote-project/build') self.assertFalse(dir_exists('build-tests/remote-project/build')) # create the build directory path os.makedirs('build-tests/remote-project/build') self.assertTrue(dir_exists('build-tests/remote-project/build')) # move the key into the build directory fr_key = FileReader('build-tests/remote-project/zip_key.yaml') zip_key_data = fr_key.read() fw_key = FileWriter('build-tests/remote-project/build/zip_key.yaml') fw_key.write(zip_key_data) # confirm that the build files are present self.assertTrue(file_exists('build-tests/remote-project/build/zip_key.yaml')) # get the expected text for outfile write assertions self.fourohfour_text = FileReader('standards/404.html').read() self.indexhtml_text = FileReader('standards/index.html').read() self.jquery_text = FileReader('standards/jquery.js').read() self.normalize_text = FileReader('standards/normalize-min.css').read()
def setUp(self): self.cwd = os.getcwd() self.remote_packagerepo_testdir = "build-tests/remote-package-repo/build" self.package_repo_keypath = "build-tests/remote-package-repo/key.yaml" self.package_repo_missing_keypath = "build-tests/remote-package-repo/key_missingpackage.yaml" # remove all contents of the build directory and build directory path from prior test run if dir_exists(self.remote_packagerepo_testdir): shutil.rmtree(self.remote_packagerepo_testdir) self.assertFalse(dir_exists(self.remote_packagerepo_testdir)) # create the build directory path with empty directory os.makedirs(self.remote_packagerepo_testdir) self.assertTrue(dir_exists(self.remote_packagerepo_testdir)) # move the key with good file path into the build directory fr_key = FileReader('build-tests/remote-package-repo/key.yaml') key_data = fr_key.read() fw_key = FileWriter('build-tests/remote-package-repo/build/key.yaml') fw_key.write(key_data) # confirm that the build files are present self.assertTrue(file_exists('build-tests/remote-package-repo/build/key.yaml'))
def test_ink_renderer_render_fail_with_incorrect_delim(self): """Confirm that Ink renderer fails with incorrect delimiter assignment""" template_string = FileReader(self.template_path).read_utf8() standard_string = FileReader(self.standard_path).read_utf8() template = Template(template_string, "[[", "]]") renderer = Renderer(template, self.key_dictionary) rendered_doc = renderer.render() self.assertNotEqual(rendered_doc, standard_string)
def test_ink_renderer_render_new_delim(self): """Test Ink render with new delimiters""" template_string = FileReader(self.template_path2).read_utf8() standard_string = FileReader(self.standard_path).read_utf8() template = Template(template_string, "[[", "]]", escape_regex=True) # have to escape special regex chars renderer = Renderer(template, self.key_dictionary) rendered_doc = renderer.render() self.assertEqual(rendered_doc, standard_string)
def test_ink_renderer_render_default_delim(self): """Test Ink render with default delimiters""" template_string = FileReader(self.template_path).read_utf8() standard_string = FileReader(self.standard_path).read_utf8() template = Template(template_string) renderer = Renderer(template, self.key_dictionary) rendered_doc = renderer.render() self.assertEqual(rendered_doc, standard_string)
def setUp(self): self.current_dir = os.getcwd() self.mit_standard = "standards/russian-mit-license.txt" self.mit_verbatim_standard = "standards/russian-mit-verbatim.txt" self.mit_test_dir = "build-tests/unicode-mit-license" self.key = "remote-rus-key.yaml" mit_std_reader = FileReader(self.mit_standard) self.mit_standard_text = mit_std_reader.read()
def test_file_gzip_utf8_readwrite_explicit_decode(self): """Test gzip compression and read from compressed unicode text file with explicit utf-8 decode""" if state.py2: FileWriter(self.unicode_path).gzip(self.unicode_string) gzip_contents = FileReader(self.unicode_path + ".gz").read_gzip("utf-8") # when read with explicit utf-8 decoding, strings should match self.assertEqual(gzip_contents, self.unicode_string) elif state.py3: FileWriter(self.unicode_path).gzip(bytes(self.unicode_string, 'utf-8')) gzip_contents = FileReader(self.unicode_path + ".gz").read_gzip("utf-8") # when read with explicit utf-8 decoding, strings should match self.assertEqual(gzip_contents, self.unicode_string)
def test_file_gzip_ascii_readwrite(self): """Test gzip compression and read from compressed ascii text file in Python 2""" if state.py2: FileWriter(self.ascii_path).gzip(self.ascii_string) gzip_contents = FileReader(self.ascii_path + ".gz").read_gzip() self.assertEqual(gzip_contents, self.ascii_string) elif state.py3: FileWriter(self.ascii_path).gzip(bytes(self.ascii_string, 'utf-8')) gzip_contents = FileReader(self.ascii_path + ".gz").read_gzip() self.assertEqual(gzip_contents.decode('ascii'), self.ascii_string)
def test_http_get_text_absent(self): """Test HTTP get request for text data and write of text file""" filepath = os.path.join('testfiles', 'testdir', 'test.txt') if file_exists(filepath): os.remove(filepath) http = HTTP("https://raw.github.com/chrissimpkins/six-four/master/LICENSE") response = http.get_txt_write_file(filepath) fr = FileReader(filepath) the_dl_text = fr.read() self.assertEqual(the_dl_text.strip(), self.http_string.strip()) #test that the file write is appropriate self.assertEqual(response, True) # test that the response from the method is correct (True on completed file write)
def test_http_post_text_file_absent(self): """Test HTTP POST request text file write when the file does not exist""" filepath = os.path.join('testfiles', 'testdir', 'post.txt') if file_exists(filepath): os.remove(filepath) http = HTTP("http://httpbin.org/post") http_text = http.post_txt_write_file(filepath) fr = FileReader(filepath) the_text = fr.read_utf8() #read file in textobj = json.loads(the_text) #convert JSON to Py object self.assertEqual(True, http_text) # test boolean for confirmation of data write self.assertEqual(textobj['url'], 'http://httpbin.org/post') #confirm the write of subset of the text
def test_unicode_templatetext_attr(self): self.maxDiff = None fr = FileReader(self.unicode_standard) std_template_text = fr.read() temp = DoxxTemplate(self.unicode_template) temp.load_data() temp.split_data() temp.parse_template_for_errors() temp.parse_template_text() self.assertEqual(std_template_text, temp.text)
def test_http_post_text_file_absent(self): """Test HTTP POST request text file write when the file does not exist""" filepath = os.path.join('testfiles', 'testdir', 'post.txt') if file_exists(filepath): os.remove(filepath) http = HTTP("http://httpbin.org/post") http_text = http.post_txt_write_file(filepath) fr = FileReader(filepath) the_text = fr.read_utf8() #read file in textobj = json.loads(the_text) #convert JSON to Py object self.assertEqual( True, http_text) # test boolean for confirmation of data write self.assertEqual(textobj['url'], 'http://httpbin.org/post' ) #confirm the write of subset of the text
def test_http_get_text_absent(self): """Test HTTP get request for text data and write of text file""" filepath = os.path.join('testfiles', 'testdir', 'test.txt') if file_exists(filepath): os.remove(filepath) http = HTTP( "https://raw.github.com/chrissimpkins/six-four/master/LICENSE") response = http.get_txt_write_file(filepath) fr = FileReader(filepath) the_dl_text = fr.read() self.assertEqual(the_dl_text.strip(), self.http_string.strip() ) #test that the file write is appropriate self.assertEqual( response, True ) # test that the response from the method is correct (True on completed file write)
def test_file_ascii_readwrite_string_type(self): FileWriter(self.ascii_path).write(self.ascii_string) # file write ascii_text = FileReader(self.ascii_path).read() # file read if state.py2: self.assertEqual(type(unicode("test string")), type(ascii_text)) #python 2 treats all input as unicode type elif state.py3: self.assertEqual(type(str("test string")), type(ascii_text)) #python 3 treats all input as str
def setUp(self): self.current_dir = os.getcwd() self.mit_standard = "standards/mit-license.txt" self.mit_verbatim_standard = "standards/mit-verbatim.txt" self.mit_test_dir = "build-tests/mit-license" self.mit_key = "key.yaml" # executed inside the mit testing directory, no directory path necessary self.mit_key_with_extension = "key-with-extension.yaml" self.mit_key_with_destdir = "key-with-destdir.yaml" self.mit_key_undefined_destdir = "key-undefined-destdir.yaml" self.mit_key_verbatim = "key-verbatim.yaml" mit_std_reader = FileReader(self.mit_standard) self.mit_standard_text = mit_std_reader.read() mit_v_reader = FileReader(self.mit_verbatim_standard) self.mit_verbatim_standard_text = mit_v_reader.read()
def test_file_readutf8_writeutf8_string_type(self): FileWriter(self.unicode_path).write_utf8(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() if state.py2: self.assertEqual(type(unicode("test string")), type(unicode_text)) # confirm that python2 treats as unicode elif state.py3: self.assertEqual(type(str("test string")), type(unicode_text)) # confirm that python3 treats as str
def test_ink_renderer_default_delimiters(self): """Test new Ink renderer assignment of default delimiters from the template""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) renderer = Renderer(template, self.key_dictionary) self.assertEqual(renderer.odel, "{{") self.assertEqual(renderer.cdel, "}}")
def test_mit_license_undefined_destdir_build(self): os.chdir(self.mit_test_dir) # cd to the mit test dir try: b = Builder(self.mit_key_undefined_destdir) b.run() self.assertTrue(file_exists('LICENSE')) # confirm that the rendered file write took place fr = FileReader('LICENSE') rendered_text = fr.read() self.assertEqual(self.mit_standard_text, rendered_text) # confirm that the rendered text matches expected text os.remove('LICENSE') # remove the rendered file to prepare directory for new tests os.chdir(self.current_dir) except Exception as e: if file_exists('LICENSE'): os.remove('LICENSE') # remove the generated LICENSE file if it is present before the exception was raised os.chdir(self.current_dir) # cd back to the main test directory before exception raised (avoids issues with other tests) raise e
def test_ink_renderer_new_delimiters(self): """Test new Ink renderer assignment of new delimiters from the template""" template_string = FileReader(self.template_path2).read_utf8() template = Template(template_string, "[[", "]]") renderer = Renderer(template, self.key_dictionary) self.assertEqual(renderer.odel, "[[") self.assertEqual(renderer.cdel, "]]")
def test_ink_make_template_varlist_new_delimiter_wrong_delim(self): """Test new Ink template variable list property assignment when new delimiter is wrong""" template_string = FileReader(self.template_path).read_utf8() template = Template( template_string, "[[", "]]", escape_regex=True) # have to escape special regex chars self.assertEqual(template.varlist, set([]))
def test_mit_license_with_destdir_build(self): os.chdir(self.mit_test_dir) # cd to the mit test dir try: b = Builder(self.mit_key_with_destdir) b.run() self.assertTrue(file_exists('build/LICENSE')) # confirm that the rendered file write took place fr = FileReader('build/LICENSE') rendered_text = fr.read() self.assertEqual(self.mit_standard_text, rendered_text) # confirm that the rendered text matches expected text import shutil shutil.rmtree('build') os.chdir(self.current_dir) except Exception as e: if file_exists('build/LICENSE'): import shutil shutil.rmtree('build') os.chdir(self.current_dir) # cd back to the main test directory before exception raised (avoids issues with other tests) raise e
def setUp(self): self.current_dir = os.getcwd() self.mit_standard = "standards/mit-license.txt" self.mit_verbatim_standard = "standards/mit-verbatim.txt" self.mit_test_dir = "build-tests/mit-license" self.remote_key = 'remote-key.yaml' self.remote_key_with_extension = 'remote-key-with-extension.yaml' self.remote_key_with_destdir = 'remote-key-with-destdir.yaml' self.remote_key_undefined_destdir = "remote-key-undefined-destdir.yaml" self.remote_key_verbatim = "remote-key-verbatim.yaml" mit_std_reader = FileReader(self.mit_standard) self.mit_standard_text = mit_std_reader.read() mit_v_reader = FileReader(self.mit_verbatim_standard) self.mit_verbatim_standard_text = mit_v_reader.read()
def test_ink_make_template_varlist_new_delimiters(self): """Test new Ink template variable list property assignment with new delimiters""" template_string = FileReader(self.template_path2).read_utf8() template = Template(template_string, "[[", "]]", escape_regex=True) # have to escape special regex chars if state.py2: # pass - need to skip this for Py3.2 tests self.assertEqual(template.varlist, set([u'appname', u'description', u'url', u'license', u'author', u'email'])) else: self.assertEqual(template.varlist, set(['appname', 'description', 'url', 'license', 'author', 'email']))
def test_ink_make_template_varlist(self): """Test new Ink template variable list property assignment""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) if state.py2: # pass - need to skip this for Py3.2 tests self.assertEqual(template.varlist, set([u'appname', u'description', u'url', u'license', u'author', u'email'])) # convert to sets to ignore order else: self.assertEqual(template.varlist, set(['appname', 'description', 'url', 'license', 'author', 'email']))
def test_http_get_text_exists_request_overwrite(self): """Test HTTP GET request with text file write does overwrite existing file when requested to do so""" filepath = os.path.join('testfiles', 'testdir', 'test.txt') http = HTTP( 'https://raw.github.com/chrissimpkins/six-four/master/LICENSE') fw = FileWriter(filepath) fw.write("test") http.get_txt_write_file(filepath, overwrite_existing=True) self.assertEqual( FileReader(filepath).read().strip(), self.http_string.strip())
def test_file_ascii_safewrite(self): """Test safe_write() to confirm does not overwrite existing file""" os.remove(self.ascii_path) #remove the existing text file for tests if os.path.exists(self.ascii_path): raise IOError("The ascii test file was not deleted. (test_IO.py.test_file_ascii_safewrite)") else: safe_response = FileWriter(self.ascii_path).safe_write(self.ascii_string) # attempt safe_write when no preexisting file present ascii_text = FileReader(self.ascii_path).read() self.assertEqual(ascii_text, self.ascii_string) # assert that the correct text was written self.assertEqual(safe_response, True) # assert that returns True when file not present and writes if os.path.exists(self.ascii_path): self.assertEqual(FileWriter(self.ascii_path).safe_write(self.ascii_string), False) #confirm that returns False to calling function when there is a pre-existing file else: raise IOError("The ascii test file is not present (test_IO.py.test_file_ascii_safewrite)")
def test_doxx_github_shortcode_branch_cherrypick_files(self): try: os.chdir(self.test_dir) # FILE with file extension run_pull(self.repo_shortcode_branch_cherry_file) self.assertTrue(file_exists("testfile.txt")) fr_tft = FileReader("testfile.txt") text_tft = fr_tft.read() self.assertEqual(u"testfile.txt branch v1.1\n", text_tft) # execute the command again and confirm that overwrite does not occur, adds -new to the filename run_pull(self.repo_shortcode_branch_cherry_file) self.assertTrue(file_exists("testfile.txt")) self.assertTrue(file_exists("testfile-new.txt")) fr_tftn = FileReader("testfile-new.txt") text_tftn = fr_tftn.read() self.assertEqual(u"testfile.txt branch v1.1\n", text_tftn) # FILE without file extension run_pull(self.repo_shortcode_branch_cherry_file_no_ext) self.assertTrue(file_exists("testfile")) fr_tf = FileReader("testfile") text_tf = fr_tf.read() self.assertEqual(u"testfile branch v1.1\n", text_tf) # execute the command again and confirm that overwrite does not occur, adds -new to the filename run_pull(self.repo_shortcode_branch_cherry_file_no_ext) self.assertTrue(file_exists("testfile")) self.assertTrue(file_exists("testfile-new")) fr_tfn = FileReader("testfile-new") text_tfn = fr_tfn.read() self.assertEqual(u"testfile branch v1.1\n", text_tfn) #cleanup os.remove("testfile.txt") os.remove("testfile-new.txt") os.remove("testfile") os.remove("testfile-new") os.chdir(self.cwd) except Exception as e: os.chdir(self.cwd) raise e
def setUp(self): self.cwd = os.getcwd() self.remote_multi_testdir = "build-tests/remote-multifile" self.remote_multi_key = "key.yaml" # executed from the same directory self.mit_path = "standards/mit-license.txt" self.mit_rus_path = "standards/russian-mit-license.txt" self.mit_verbatim_path = "standards/mit-verbatim.txt" fr_mit = FileReader(self.mit_path) fr_rusmit = FileReader(self.mit_rus_path) fr_verbatim = FileReader(self.mit_verbatim_path) self.mit_text = fr_mit.read() self.mit_rus_text = fr_rusmit.read() self.mit_verbatim_text = fr_verbatim.read()
def test_multifile_remote_build(self): try: os.chdir(self.remote_multi_testdir) b = Builder(self.remote_multi_key) b.run() # assert that files were made self.assertTrue(file_exists('mit.txt')) self.assertTrue(file_exists('rus-mit.txt')) self.assertTrue(file_exists('mit-verbatim')) # uses 'extension: ' in the template metadata fr_m = FileReader('mit.txt') fr_r = FileReader('rus-mit.txt') fr_v = FileReader('mit-verbatim') rendered_mit_text = fr_m.read() rendered_rus_text = fr_r.read() rendered_ver_text = fr_v.read() # assert that the rendered text is as expected self.assertEqual(self.mit_text, rendered_mit_text) self.assertEqual(self.mit_rus_text, rendered_rus_text) self.assertEqual(self.mit_verbatim_text, rendered_ver_text) # remove the rendered files os.remove('mit.txt') os.remove('rus-mit.txt') os.remove('mit-verbatim') os.chdir(self.cwd) except Exception as e: if file_exists('mit.txt'): os.remove('mit.txt') if file_exists('rus-mit.txt'): os.remove('rus-mit.txt') if file_exists('mit-verbatim'): os.remove('mit-verbatim') os.chdir(self.cwd) raise e
def test_file_utf8_readwrite(self): """Test write and read of a utf-8 encoded file""" FileWriter(self.unicode_path).write_utf8(self.unicode_string) unicode_text = FileReader(self.unicode_path).read_utf8() self.assertEqual(unicode_text, self.unicode_string)
def test_ink_renderer_key_dictionary(self): """Test new Ink renderer key_dict property""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) renderer = Renderer(template, self.key_dictionary) self.assertEqual(renderer.key_dict, self.key_dictionary)
def test_ink_renderer_template_property_string(self): """Test new Ink renderer template property is a string""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) renderer = Renderer(template, self.key_dictionary) self.assertIsInstance(renderer.template, str)
def _read_text_file(self, file_path): try: fr = FileReader(file_path) return fr.read() # return the file text except Exception: return "" # return empty string if there is an exception during the read
def test_file_bin_read_unicode_as_bin(self): """Test read of unicode as binary with decode""" FileWriter(self.unicode_path).write_utf8(self.unicode_string) bin_data = FileReader(self.unicode_path).read_bin() #read unicode file as binary uni_text = bin_data.decode("utf-8") #decode to utf-8 self.assertEqual(uni_text, self.unicode_string)
def test_file_ascii_readwrite(self): """Test write and read of ascii encoded file""" FileWriter(self.ascii_path).write(self.ascii_string) # file write ascii_text = FileReader(self.ascii_path).read() # file read self.assertEqual(ascii_text, self.ascii_string)
def test_ink_make_template_varlist_default_delim_wrong_delim(self): """Test new Ink template variable list property assignment when default delimiter is incorrect""" template_string = FileReader( self.template_path2).read_utf8() # uses the [[ & ]] delimiters template = Template(template_string) self.assertEqual(template.varlist, set([]))
def test_ink_make_template_new_delimiters_without_escape(self): """test new Ink template delimiter properties assignment fails without proper regex escape""" template_string = FileReader(self.template_path).read_utf8() self.assertRaises(TypeError, Template(template_string, "[[", "]]"))
def test_ink_make_template_default_delimiter(self): """Test default Ink template delimiter properties assignment""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) self.assertEqual(template.odel, "{{") self.assertEqual(template.cdel, "}}")
def test_ink_make_template_new_delimiters(self): """Test new Ink template delimiter properties assignment""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string, "[[", "]]", escape_regex=True) self.assertEqual(template.odel, "[[") self.assertEqual(template.cdel, "]]")
def generate_hash(filepath): """Public function that reads a local file and generates a SHA256 hash digest for it""" fr = FileReader(filepath) data = fr.read_bin() return _calculate_sha256(data)
def test_ink_make_template_string(self): """Test new Ink template string assignment""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) self.assertEqual(template, template_string)
def test_ink_template_type_string(self): """Test that Ink template is of type string""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) self.assertIsInstance(template, str)
def test_file_ascii_readwrite_append(self): """Test append of ascii text to existing file""" FileWriter(self.ascii_path).append(self.ascii_string) #append a second string of the ascii text ascii_text = FileReader(self.ascii_path).read() self.assertEqual(ascii_text, (self.ascii_string)*2) #confirm that it equals two of the ascii strings
def test_ink_template_varlist_type_set(self): """Test that the Ink template variable list is of type set""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) self.assertIsInstance(template.varlist, set)
def test_ink_template_string_method(self): """Test that a slice string method works on the Ink template""" template_string = FileReader(self.template_path).read_utf8() template = Template(template_string) self.assertEqual(template[0:5], "impor")