blob: aefe38b9aa24cd5e1f90184cf4190f538d5f5074 [file] [log] [blame]
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17#include "find.h"
18
Martin Hsu616f3512018-12-06 11:18:42 +080019#include <stdlib.h>
Dan Willemsen78936012017-07-25 23:35:05 -070020#include <unistd.h>
Dan Willemsen3ce083f2017-10-11 22:17:48 -070021#include <string>
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +090022
Dan Willemsen78936012017-07-25 23:35:05 -070023#include "fileutil.h"
Shinichiro Hamaji171dc822015-10-08 16:40:21 +090024#include "strutil.h"
25
Dan Willemsen78936012017-07-25 23:35:05 -070026int FindUnitTests();
27
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +090028int main(int argc, char* argv[]) {
29 if (argc == 1) {
Dan Willemsen78936012017-07-25 23:35:05 -070030 return FindUnitTests();
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +090031 }
32
33 InitFindEmulator();
34 string cmd;
35 for (int i = 1; i < argc; i++) {
36 if (i > 1)
37 cmd += ' ';
38 cmd += argv[i];
39 }
40 FindCommand fc;
41 if (!fc.Parse(cmd)) {
42 fprintf(stderr, "Find emulator does not support this command\n");
43 return 1;
44 }
45 string out;
Dan Willemsen692e64e2017-02-22 15:38:33 -080046 if (!FindEmulator::Get()->HandleFind(cmd, fc, Loc(), &out)) {
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +090047 fprintf(stderr, "Find emulator does not support this command\n");
48 return 1;
49 }
Shinichiro Hamaji171dc822015-10-08 16:40:21 +090050
51 for (StringPiece tok : WordScanner(out)) {
52 printf("%.*s\n", SPF(tok));
53 }
Shinichiro Hamaji3c509d92015-09-08 13:34:51 +090054}
Dan Willemsen78936012017-07-25 23:35:05 -070055
56string Run(const string& cmd) {
57 string s;
58 int ret = RunCommand("/bin/sh", "-c", cmd, RedirectStderr::NONE, &s);
59
60 if (ret != 0) {
61 fprintf(stderr, "Failed to run `%s`\n", cmd.c_str());
62 exit(ret);
63 }
64
65 return s;
66}
67
68static bool unit_test_failed = false;
69
70void CompareFind(const string& cmd) {
71 string native = Run(cmd);
72
73 FindCommand fc;
74 if (!fc.Parse(cmd)) {
75 fprintf(stderr, "Find emulator cannot parse `%s`\n", cmd.c_str());
76 exit(1);
77 }
78 string emulated;
79 if (!FindEmulator::Get()->HandleFind(cmd, fc, Loc(), &emulated)) {
80 fprintf(stderr, "Find emulator cannot parse `%s`\n", cmd.c_str());
81 exit(1);
82 }
83
84 vector<StringPiece> nativeWords;
85 vector<StringPiece> emulatedWords;
86
87 WordScanner(native).Split(&nativeWords);
88 WordScanner(emulated).Split(&emulatedWords);
89
90 if (nativeWords != emulatedWords) {
91 fprintf(stderr, "Failed to match `%s`:\n", cmd.c_str());
92
93 auto nativeIter = nativeWords.begin();
94 auto emulatedIter = emulatedWords.begin();
95 fprintf(stderr, "%-20s %-20s\n", "Native:", "Emulated:");
Dan Willemsen3ce083f2017-10-11 22:17:48 -070096 while (nativeIter != nativeWords.end() ||
97 emulatedIter != emulatedWords.end()) {
Dan Willemsen78936012017-07-25 23:35:05 -070098 fprintf(stderr, " %-20s %-20s\n",
Dan Willemsen3ce083f2017-10-11 22:17:48 -070099 (nativeIter == nativeWords.end())
100 ? ""
101 : (*nativeIter++).as_string().c_str(),
102 (emulatedIter == emulatedWords.end())
103 ? ""
104 : (*emulatedIter++).as_string().c_str());
Dan Willemsen78936012017-07-25 23:35:05 -0700105 }
106 fprintf(stderr, "------------------------------------------\n");
107 unit_test_failed = true;
108 }
109}
110
Dan Willemsen1e015922018-02-12 18:16:35 -0800111void ExpectParseFailure(const string& cmd) {
112 Run(cmd);
113
114 FindCommand fc;
115 if (fc.Parse(cmd)) {
116 fprintf(stderr, "Expected parse failure for `%s`\n", cmd.c_str());
117 fprintf(stderr, "------------------------------------------\n");
118 unit_test_failed = true;
119 }
120}
121
Dan Willemsen78936012017-07-25 23:35:05 -0700122int FindUnitTests() {
123 Run("rm -rf out/find");
124 Run("mkdir -p out/find");
125 if (chdir("out/find")) {
126 perror("Failed to chdir(out/find)");
127 return 1;
128 }
129
130 // Set up files under out/find:
131 // drwxr-x--- top
132 // lrwxrwxrwx top/E -> missing
133 // lrwxrwxrwx top/C -> A
134 // -rw-r----- top/a
135 // drwxr-x--- top/A
136 // lrwxrwxrwx top/A/D -> B
137 // -rw-r----- top/A/b
138 // drwxr-x--- top/A/B
139 // -rw-r----- top/A/B/z
140 Run("mkdir -p top/A/B");
141 Run("cd top && ln -s A C");
142 Run("cd top/A && ln -s B D");
143 Run("cd top && ln -s missing E");
144 Run("touch top/a top/A/b top/A/B/z");
145
146 InitFindEmulator();
147
148 CompareFind("find .");
149 CompareFind("find -L .");
150
Dan Willemsen4be0f092017-07-25 20:26:54 -0700151 CompareFind("find top/C");
152 CompareFind("find top/C/.");
153 CompareFind("find -L top/C");
154 CompareFind("find -L top/C/.");
155
Dan Willemsen78936012017-07-25 23:35:05 -0700156 CompareFind("cd top && find C");
157 CompareFind("cd top && find -L C");
Dan Willemsen4be0f092017-07-25 20:26:54 -0700158 CompareFind("cd top/C && find .");
Dan Willemsen78936012017-07-25 23:35:05 -0700159
160 CompareFind("cd top/C && find D/./z");
161
Dan Willemsen4be0f092017-07-25 20:26:54 -0700162 CompareFind("find .//top");
163
Dan Willemsen1e015922018-02-12 18:16:35 -0800164 CompareFind("find top -type f -name 'a*' -o -name \\*b");
165 CompareFind("find top \\! -name 'a*'");
166 CompareFind("find top \\( -name 'a*' \\)");
167
168 ExpectParseFailure("find top -name a\\*");
169
Dan Willemsen78936012017-07-25 23:35:05 -0700170 return unit_test_failed ? 1 : 0;
171}