From 0f0a5e92d35b609fa5630ffd4eb51f67bc01053d Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 13 Jan 2014 22:37:33 +0400 Subject: [PATCH] Replace sed with python script sed escaping is wrong: e.g. re.escape() will escape `+`, but with BRE escaped plus is quantifier --- tests/test_shells/postproc.py | 32 ++++++++++++++++++++++++++++++++ tests/test_shells/test.sh | 8 ++------ tests/test_shells/zsh.ok | 1 + 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100755 tests/test_shells/postproc.py diff --git a/tests/test_shells/postproc.py b/tests/test_shells/postproc.py new file mode 100755 index 00000000..8cd037d0 --- /dev/null +++ b/tests/test_shells/postproc.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +from __future__ import unicode_literals + +import os +import socket +import sys +import codecs + + +fname = sys.argv[1] +new_fname = fname + '.new' +pid_fname = 'tests/shell/3rd/pid' + +with open(pid_fname, 'r') as P: + pid = P.read().strip() +hostname = socket.gethostname() +user = os.environ['USER'] + +with codecs.open(fname, 'r', encoding='utf-8') as R: + with codecs.open(new_fname, 'w', encoding='utf-8') as W: + found_cd = False + for line in R: + if not found_cd: + found_cd = ('cd tests/shell/3rd' in line) + continue + line = line.replace(pid, 'PID') + line = line.replace(hostname, 'HOSTNAME') + line = line.replace(user, 'USER') + W.write(line) + +os.rename(new_fname, fname) diff --git a/tests/test_shells/test.sh b/tests/test_shells/test.sh index ca83d864..1459da5e 100755 --- a/tests/test_shells/test.sh +++ b/tests/test_shells/test.sh @@ -24,12 +24,8 @@ run_test() { while screen -S "$SESNAME" -X blankerprg "" > /dev/null ; do sleep 1s done - sed -i -e "1,3 d" \ - -e s/$(cat tests/shell/3rd/pid)/PID/g \ - -e "s/$(python -c 'import re, socket; print (re.escape(socket.gethostname()))')/HOSTNAME/g" \ - -e "s/$(python -c 'import os, re; print (re.escape(os.environ["USER"]))')/USER/g" \ - tests/shell/screen.log - if ! diff -u tests/test_shells/${SH}.ok tests/shell/screen.log ; then + ./tests/test_shells/postproc.py tests/shell/screen.log + if ! diff -u tests/test_shells/${SH}.ok tests/shell/screen.log | cat -v ; then return 1 fi return 0 diff --git a/tests/test_shells/zsh.ok b/tests/test_shells/zsh.ok index a986ed4d..74d885cf 100644 --- a/tests/test_shells/zsh.ok +++ b/tests/test_shells/zsh.ok @@ -1,3 +1,4 @@ +  USER  ⋯  tests  shell  3rd  POWERLINE_COMMAND=( $POWERLINE_COMMAND -c ext.shell.theme=default_leftonly )  USER   BRANCH  ⋯  tests  shell  3rd  cd .git  USER   BRANCH  ⋯  shell  3rd  .git  cd ..