Esempio n. 1
0
    def test_success(self):
        '''
        正常系: 単一の行に目当ての文字列が含まれている
        '''
        text = 'ほげほげ。更に、ふがふが'
        expect = 'ほげほげ。さらに、ふがふが'

        result = replace(text)
        eq_(result, expect)
Esempio n. 2
0
    def test_success_multiple_lines(self):
        '''
        正常系: 複数行に目当ての文字列が含まれている
        '''
        text = '''ほげほげ
更に、ふがふが
更に、ばずばず
'''
        expect = '''ほげほげ
さらに、ふがふが
さらに、ばずばず
'''

        result = replace(text)
        eq_(result, expect)
Esempio n. 3
0
def _replace(text, filepath, encoding):
    # FIXME: ファイルごとに一時ディレクトリを作るのは効率が悪すぎる
    temp_dirpath = tempfile.mkdtemp()

    try:
        shutil.copy(filepath, temp_dirpath)
        filename = os.path.basename(filepath)
        temp_filepath = os.path.join(temp_dirpath, filename)
        encoding = util.get_encoding(temp_filepath)

        replaced_text = api.replace(text)
        util.write(temp_filepath, replaced_text, encoding)

        os.rename(temp_filepath, filepath)
    finally:
        shutil.rmtree(temp_dirpath, ignore_errors=True)