Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestStripShellComment(t *testing.T) { |
| 6 | for _, tc := range []struct { |
| 7 | in string |
| 8 | want string |
| 9 | }{ |
| 10 | { |
| 11 | in: `foo`, |
| 12 | want: `foo`, |
| 13 | }, |
| 14 | { |
| 15 | in: `foo # bar`, |
| 16 | want: `foo `, |
| 17 | }, |
| 18 | { |
| 19 | in: `foo '# bar'`, |
| 20 | want: `foo '# bar'`, |
| 21 | }, |
| 22 | { |
| 23 | in: `foo '\'# bar'`, |
| 24 | want: `foo '\'`, |
| 25 | }, |
| 26 | { |
| 27 | in: `foo "# bar"`, |
| 28 | want: `foo "# bar"`, |
| 29 | }, |
| 30 | { |
| 31 | in: `foo "\"# bar"`, |
| 32 | want: `foo "\"# bar"`, |
| 33 | }, |
| 34 | { |
| 35 | in: `foo "\\"# bar"`, |
| 36 | want: `foo "\\"`, |
| 37 | }, |
| 38 | { |
| 39 | in: "foo `# bar`", |
| 40 | want: "foo `# bar`", |
| 41 | }, |
| 42 | { |
| 43 | in: "foo `\\`# bar`", |
| 44 | want: "foo `\\`# bar`", |
| 45 | }, |
| 46 | { |
| 47 | in: "foo `\\\\`# bar`", |
| 48 | want: "foo `\\\\`", |
| 49 | }, |
| 50 | } { |
| 51 | got := stripShellComment(tc.in) |
| 52 | if got != tc.want { |
| 53 | t.Errorf(`stripShellComment(%q)=%q, want %q`, tc.in, got, tc.want) |
| 54 | } |
| 55 | } |
| 56 | } |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame^] | 57 | |
| 58 | func TestGetDepFile(t *testing.T) { |
| 59 | for _, tc := range []struct { |
| 60 | in string |
| 61 | want string |
| 62 | err bool |
| 63 | }{ |
| 64 | { |
| 65 | in: `g++ -c fat.cc -o fat.o`, |
| 66 | want: ``, |
| 67 | }, |
| 68 | { |
| 69 | in: `g++ -c fat.cc -MD`, |
| 70 | err: true, |
| 71 | }, |
| 72 | { |
| 73 | in: `g++ -c fat.cc -MD -o fat.o -o fat.o`, |
| 74 | err: true, |
| 75 | }, |
| 76 | { |
| 77 | in: `g++ -c fat.cc -MD -o fat.o`, |
| 78 | want: `fat.d`, |
| 79 | }, |
| 80 | { |
| 81 | in: `g++ -c fat.cc -MD -o fat`, |
| 82 | want: `fat.d`, |
| 83 | }, |
| 84 | { |
| 85 | in: `g++ -c fat.cc -MD -MF foo.d -o fat.o`, |
| 86 | want: `foo.d`, |
| 87 | }, |
| 88 | { |
| 89 | in: `g++ -c fat.cc -MD -o fat.o -MF foo.d`, |
| 90 | want: `foo.d`, |
| 91 | }, |
| 92 | // A real example from maloader. |
| 93 | { |
| 94 | in: `g++ -g -Iinclude -Wall -MMD -fno-omit-frame-pointer -O -m64 -W -Werror -c -o fat.o fat.cc`, |
| 95 | want: `fat.d`, |
| 96 | }, |
| 97 | // A real example from Android. |
| 98 | { |
| 99 | in: `mkdir -p out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/ && echo "host C++: llvm-rs-cc <= frameworks/compile/slang/llvm-rs-cc.cpp" && prebuilts/clang/linux-x86/host/3.6/bin/clang++ -I external/llvm -I external/llvm/include -I external/llvm/host/include -I external/clang/include -I external/clang/lib/CodeGen -I frameworks/compile/libbcc/include -I out/host/linux-x86/gen/EXECUTABLES/llvm-rs-cc_intermediates/include -I external/libcxx/include -I frameworks/compile/slang -I out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates -I out/host/linux-x86/gen/EXECUTABLES/llvm-rs-cc_intermediates -I libnativehelper/include/nativehelper $(cat out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/import_includes) -isystem system/core/include -isystem hardware/libhardware/include -isystem hardware/libhardware_legacy/include -isystem hardware/ril/include -isystem libnativehelper/include -isystem frameworks/native/include -isystem frameworks/native/opengl/include -isystem frameworks/av/include -isystem frameworks/base/include -isystem tools/include -isystem out/host/linux-x86/obj/include -c -fno-exceptions -Wno-multichar -m64 -Wa,--noexecstack -fPIC -no-canonical-prefixes -include build/core/combo/include/arch/linux-x86/AndroidConfig.h -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -O2 -g -fno-strict-aliasing -DNDEBUG -UDEBUG -D__compiler_offsetof=__builtin_offsetof -Werror=int-conversion -Wno-unused-command-line-argument --gcc-toolchain=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/ --gcc-toolchain=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/ --sysroot=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8//sysroot -target x86_64-linux-gnu -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Wsign-promo -std=gnu++11 -DNDEBUG -UDEBUG -Wno-inconsistent-missing-override --gcc-toolchain=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/ --sysroot=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8//sysroot -isystem prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8//x86_64-linux/include/c++/4.8 -isystem prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8//x86_64-linux/include/c++/4.8/x86_64-linux -isystem prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8//x86_64-linux/include/c++/4.8/backward -target x86_64-linux-gnu -pedantic -Wcast-qual -Wno-long-long -Wno-sign-promo -Wall -Wno-unused-parameter -Wno-return-type -Werror -std=c++11 -O0 -DTARGET_BUILD_VARIANT=eng -DRS_VERSION=23 -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -O2 -fomit-frame-pointer -Wall -W -Wno-unused-parameter -Wwrite-strings -Dsprintf=sprintf -pedantic -Wcast-qual -Wno-long-long -Wno-sign-promo -Wall -Wno-unused-parameter -Wno-return-type -Werror -std=c++11 -O0 -DTARGET_BUILD_VARIANT=eng -DRS_VERSION=23 -fno-exceptions -fpie -D_USING_LIBCXX -Wno-sign-promo -fno-rtti -Woverloaded-virtual -Wno-sign-promo -std=c++11 -nostdinc++ -MD -MF out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.d -o out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.o frameworks/compile/slang/llvm-rs-cc.cpp && cp out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.d out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.P; sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$//' -e '/^$/ d' -e 's/$/ :/' < out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.d >> out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.P; rm -f out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.d`, |
| 100 | want: `out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/llvm-rs-cc.P`, |
| 101 | }, |
| 102 | } { |
| 103 | got, err := getDepfile(tc.in) |
| 104 | if got != tc.want { |
| 105 | t.Errorf(`stripShellComment(%q)=%q, want %q`, tc.in, got, tc.want) |
| 106 | } |
| 107 | if tc.err && err == nil { |
| 108 | t.Errorf(`stripShellComment(%q) unexpectedly has no error`, tc.in) |
| 109 | } else if !tc.err && err != nil { |
| 110 | t.Errorf(`stripShellComment(%q) has an error: %q`, err) |
| 111 | } |
| 112 | } |
| 113 | } |