def test_replace_in_ltimes(temp_file): fp = temp_file('-up br -d br -t br') pattern = re.compile('br') replace_in(fp, pattern, 'X', ltimes=1, assert_blocks=False) assert read_file(fp) == "-up X -d br -t br" replace_in(fp, pattern, 'X', assert_blocks=False) assert read_file(fp) == "-up X -d X -t X"
def test_replace_in_no_outside(temp_file): original = textwrap.dedent(f""" interface eth0 {START_BLOCK} interface wlan0 {END_BLOCK} """) fp = temp_file(original) pattern = re.compile(r'^(interface )(.*?)$') replace_in(fp, pattern, replace=r'\1wlan1', assert_blocks=True) assert read_file(fp) == original.replace('wlan0', 'wlan1')
def test_replace_in_ntimes(temp_file): original = textwrap.dedent( """ alias BYE="/QUIT" alias BYE="/QUITT" alias BYE= """) fp = temp_file(original) replace_in( fp, re.compile(r'alias BYE=.*?$'), '', ntimes=2, assert_blocks=False) content = read_file(fp) assert content == "\n\n\nalias BYE=\n"
def test_replace_in_exceptions(temp_file): fp = temp_file('»') patt = re.compile('x') with pytest.raises(ValueError): replace_in(fp, 'x', 'y', assert_blocks=True) with pytest.raises(MissingEditBlockException): replace_in(fp, patt, 'y', assert_blocks=True) fp = temp_file(START_BLOCK + "\ncorrupted") with pytest.raises(MissingEditBlockException): replace_in(fp, patt, 'y', assert_blocks=True)
def test_replace_in_blocks(block, assert_blocks, temp_file): original = with_blocks('content') fp = temp_file(original) replace_in(fp, re.compile(block), 'X', assert_blocks=assert_blocks) assert read_file(fp) == original
def test_replace_in(pattern, replace, output, blocks, temp_file): fp = temp_file(with_blocks('custom\n# \nkey: val')) replace_in(fp, re.compile(pattern), replace, assert_blocks=blocks) assert read_file(fp) == output