]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Improve test cases that use objdump
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 1 Sep 2020 17:48:24 +0000 (19:48 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 1 Sep 2020 17:58:46 +0000 (19:58 +0200)
- Added expect_objdump_contains and expect_objdump_not_contains utility
  functions to the main test framework, making objdump_cmd and grep_cmd
  (renamed to objdump_grep_cmd) internal utility functions.
- Removed strange support for the $HOST_OS_WINDOWS || $HOST_OS_CYGWIN
  case in objdump_grep_cmd. Let’s re-add it later if and when it becomes
  a problem.
- objdump_grep_cmd now greps for literal strings, thus no longer
  interpreting $PWD as a regex.
- Made test cases use expect_objdump_(not_)contains instead of
  “objdump_grep_cmd | grep_cmd”.

test/run
test/suites/debug_prefix_map.bash
test/suites/split_dwarf.bash

index 2bc580dbe2c363f1d42ed347a7f229c7ada7ddcc..e0e9e3af339f6d519ec50a899c5236b10a12d896 100755 (executable)
--- a/test/run
+++ b/test/run
@@ -124,12 +124,24 @@ file_size() {
 }
 
 objdump_cmd() {
+    local file="$1"
+
     if $HOST_OS_APPLE; then
-        xcrun dwarfdump -r 0 $1
+        xcrun dwarfdump -r 0 "$file"
     elif $HOST_OS_WINDOWS || $HOST_OS_CYGWIN; then
-        strings $1 # for some reason objdump only shows the basename of the file, so fall back to brute force and ignorance
+        # For some reason objdump only shows the basename of the file, so fall
+        # back to brute force and ignorance.
+        strings "$1"
+    else
+        objdump -W "$file"
+    fi
+}
+
+objdump_grep_cmd() {
+    if $HOST_OS_APPLE; then
+        fgrep -q "\"$1\""
     else
-        objdump -W $1
+        fgrep -q ": $1"
     fi
 }
 
@@ -250,6 +262,24 @@ expect_not_contains() {
     fi
 }
 
+expect_objdump_contains() {
+    local file="$1"
+    local string="$2"
+
+    if ! objdump_cmd "$file" | objdump_grep_cmd "$string"; then
+        test_failed "File $file does not contain \"$string\""
+    fi
+}
+
+expect_objdump_not_contains() {
+    local file="$1"
+    local string="$2"
+
+    if objdump_cmd "$file" | objdump_grep_cmd "$string"; then
+        test_failed "File $file contains \"$string\""
+    fi
+}
+
 expect_file_count() {
     local expected=$1
     local pattern=$2
index 688daded4874626544ab48eb75506929f29f3352..ce36bfb5aed5675ce441a9adf2a18383b0802f12 100644 (file)
@@ -20,66 +20,44 @@ EOF
     backdate dir1/include/test.h dir2/include/test.h
 }
 
-grep_cmd() {
-    if $HOST_OS_APPLE; then
-        grep \"$1\"
-    elif $HOST_OS_WINDOWS || $HOST_OS_CYGWIN; then
-        test -n "$2" && grep -E "$1|$2" || grep "$1" # accept a relative path for source code, in addition to relocation dir
-    else
-        grep ": $1[[:space:]]*$"
-    fi
-}
-
 SUITE_debug_prefix_map() {
     # -------------------------------------------------------------------------
     TEST "Mapping of debug info CWD"
 
     cd dir1
-    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -I`pwd`/include -g -fdebug-prefix-map=`pwd`=dir -c `pwd`/src/test.c -o `pwd`/test.o
+    CCACHE_BASEDIR=$(pwd) $CCACHE_COMPILE -I$(pwd)/include -g -fdebug-prefix-map=$(pwd)=some_name_not_likely_to_exist_in_path -c $(pwd)/src/test.c -o $(pwd)/test.o
     expect_stat 'cache hit (direct)' 0
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o | grep_cmd "`pwd`" >/dev/null 2>&1; then
-        test_failed "Source dir (`pwd`) found in test.o"
-    fi
-    if ! objdump_cmd test.o | grep_cmd "dir" src/test.c >/dev/null 2>&1; then
-        test_failed "Relocation (dir) not found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
+    expect_objdump_contains test.o some_name_not_likely_to_exist_in_path
 
     cd ../dir2
-    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -I`pwd`/include -g -fdebug-prefix-map=`pwd`=dir -c `pwd`/src/test.c -o `pwd`/test.o
+    CCACHE_BASEDIR=$(pwd) $CCACHE_COMPILE -I$(pwd)/include -g -fdebug-prefix-map=$(pwd)=some_name_not_likely_to_exist_in_path -c $(pwd)/src/test.c -o $(pwd)/test.o
     expect_stat 'cache hit (direct)' 1
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o | grep_cmd "`pwd`" >/dev/null 2>&1; then
-        test_failed "Source dir (`pwd`) found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
 
     # -------------------------------------------------------------------------
     TEST "Multiple -fdebug-prefix-map"
 
     cd dir1
-    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -I`pwd`/include -g -fdebug-prefix-map=`pwd`=name -fdebug-prefix-map=foo=bar -c `pwd`/src/test.c -o `pwd`/test.o
+    CCACHE_BASEDIR=$(pwd) $CCACHE_COMPILE -I$(pwd)/include -g -fdebug-prefix-map=$(pwd)=some_name_not_likely_to_exist_in_path -fdebug-prefix-map=foo=bar -c $(pwd)/src/test.c -o $(pwd)/test.o
     expect_stat 'cache hit (direct)' 0
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o | grep_cmd "`pwd`" >/dev/null 2>&1; then
-        test_failed "Source dir (`pwd`) found in test.o"
-    fi
-    if ! objdump_cmd test.o | grep_cmd "name" src/test.c >/dev/null 2>&1; then
-        test_failed "Relocation (name) not found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
+    expect_objdump_contains test.o some_name_not_likely_to_exist_in_path
 
     cd ../dir2
-    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -I`pwd`/include -g -fdebug-prefix-map=`pwd`=name -fdebug-prefix-map=foo=bar -c `pwd`/src/test.c -o `pwd`/test.o
+    CCACHE_BASEDIR=$(pwd) $CCACHE_COMPILE -I$(pwd)/include -g -fdebug-prefix-map=$(pwd)=some_name_not_likely_to_exist_in_path -fdebug-prefix-map=foo=bar -c $(pwd)/src/test.c -o $(pwd)/test.o
     expect_stat 'cache hit (direct)' 1
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o | grep_cmd "`pwd`" >/dev/null 2>&1; then
-        test_failed "Source dir (`pwd`) found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
 }
index f912aa6167d7ba9568bac95b2b85f9065a49e8c9..fb13f59146b91ef87739c99982bfaaf8a173fdc7 100644 (file)
@@ -106,9 +106,7 @@ SUITE_split_dwarf() {
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o 2>/dev/null | grep_cmd "$(pwd)" >/dev/null 2>&1; then
-        test_failed "Source dir ($(pwd)) found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
 
     cd ../dir2
     CCACHE_BASEDIR=$(pwd) $CCACHE_COMPILE -I$(pwd)/include -gsplit-dwarf -fdebug-prefix-map=$(pwd)=. -c $(pwd)/src/test.c -o $(pwd)/test.o
@@ -116,9 +114,7 @@ SUITE_split_dwarf() {
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 2
-    if objdump_cmd test.o 2>/dev/null | grep_cmd "$(pwd)" >/dev/null 2>&1; then
-        test_failed "Source dir ($(pwd)) found in test.o"
-    fi
+    expect_objdump_not_contains test.o "$(pwd)"
 
     # -------------------------------------------------------------------------
     TEST "-gsplit-dwarf -g1"