comparison test/org/tmatesoft/hg/test/TestStatus.java @ 360:150500515714

Report non-critical errors during status operation to handler/inspector
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 08 Dec 2011 22:19:27 +0100
parents 7a8e1a305a78
children 155c1893bda4
comparison
equal deleted inserted replaced
359:1d9bcab9c50f 360:150500515714
24 24
25 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.Collection; 26 import java.util.Collection;
27 import java.util.Collections; 27 import java.util.Collections;
28 import java.util.HashMap; 28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
29 import java.util.LinkedList; 30 import java.util.LinkedList;
30 import java.util.List; 31 import java.util.List;
31 import java.util.Map; 32 import java.util.Map;
32 import java.util.TreeMap; 33 import java.util.TreeMap;
33 34
36 import org.junit.Rule; 37 import org.junit.Rule;
37 import org.junit.Test; 38 import org.junit.Test;
38 import org.tmatesoft.hg.core.HgStatus; 39 import org.tmatesoft.hg.core.HgStatus;
39 import org.tmatesoft.hg.core.HgStatus.Kind; 40 import org.tmatesoft.hg.core.HgStatus.Kind;
40 import org.tmatesoft.hg.core.HgStatusCommand; 41 import org.tmatesoft.hg.core.HgStatusCommand;
42 import org.tmatesoft.hg.core.HgStatusHandler;
41 import org.tmatesoft.hg.internal.PathGlobMatcher; 43 import org.tmatesoft.hg.internal.PathGlobMatcher;
42 import org.tmatesoft.hg.repo.HgLookup; 44 import org.tmatesoft.hg.repo.HgLookup;
43 import org.tmatesoft.hg.repo.HgRepository; 45 import org.tmatesoft.hg.repo.HgRepository;
44 import org.tmatesoft.hg.repo.HgStatusCollector; 46 import org.tmatesoft.hg.repo.HgStatusCollector;
45 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector; 47 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
46 import org.tmatesoft.hg.util.Path; 48 import org.tmatesoft.hg.util.Path;
49 import org.tmatesoft.hg.util.Status;
47 50
48 51
49 /** 52 /**
50 * 53 *
51 * @author Artem Tikhomirov 54 * @author Artem Tikhomirov
171 report("status -A --change " + revision, r); 174 report("status -A --change " + revision, r);
172 175
173 // TODO check not -A, but defaults()/custom set of modifications 176 // TODO check not -A, but defaults()/custom set of modifications
174 } 177 }
175 178
176 private static class StatusCollector implements HgStatusCommand.Handler { 179 private static class StatusCollector implements HgStatusHandler {
177 private final Map<Kind, List<Path>> kind2names = new TreeMap<Kind, List<Path>>(); 180 private final Map<Kind, List<Path>> kind2names = new TreeMap<Kind, List<Path>>();
178 private final Map<Path, List<Kind>> name2kinds = new TreeMap<Path, List<Kind>>(); 181 private final Map<Path, List<Kind>> name2kinds = new TreeMap<Path, List<Kind>>();
182 private final Map<Path, Status> name2error = new LinkedHashMap<Path, Status>();
179 183
180 public void handleStatus(HgStatus s) { 184 public void handleStatus(HgStatus s) {
181 List<Path> l = kind2names.get(s.getKind()); 185 List<Path> l = kind2names.get(s.getKind());
182 if (l == null) { 186 if (l == null) {
183 kind2names.put(s.getKind(), l = new LinkedList<Path>()); 187 kind2names.put(s.getKind(), l = new LinkedList<Path>());
189 name2kinds.put(s.getPath(), k = new LinkedList<Kind>()); 193 name2kinds.put(s.getPath(), k = new LinkedList<Kind>());
190 } 194 }
191 k.add(s.getKind()); 195 k.add(s.getKind());
192 } 196 }
193 197
198 public void handleError(Path file, Status s) {
199 name2error.put(file, s);
200 }
201
194 public List<Path> get(Kind k) { 202 public List<Path> get(Kind k) {
195 List<Path> rv = kind2names.get(k); 203 List<Path> rv = kind2names.get(k);
196 return rv == null ? Collections.<Path>emptyList() : rv; 204 return rv == null ? Collections.<Path>emptyList() : rv;
197 } 205 }
198 206
199 public List<Kind> get(Path p) { 207 public List<Kind> get(Path p) {
200 List<Kind> rv = name2kinds.get(p); 208 List<Kind> rv = name2kinds.get(p);
201 return rv == null ? Collections.<Kind>emptyList() : rv; 209 return rv == null ? Collections.<Kind>emptyList() : rv;
202 } 210 }
211
212 public Map<Path, Status> getErrors() {
213 return name2error;
214 }
203 } 215 }
204 216
205 /* 217 /*
206 * status-1/dir/file5 was added in rev 8, scheduled (hg remove file5) for removal, but not yet committed 218 * status-1/dir/file5 was added in rev 8, scheduled (hg remove file5) for removal, but not yet committed
207 * Erroneously reported extra REMOVED file (the one added and removed in between). Shall not 219 * Erroneously reported extra REMOVED file (the one added and removed in between). Shall not
211 // check very end of WCStatusCollector, foreach left knownEntry, collect == null || baseRevFiles.contains() 223 // check very end of WCStatusCollector, foreach left knownEntry, collect == null || baseRevFiles.contains()
212 repo = Configuration.get().find("status-1"); 224 repo = Configuration.get().find("status-1");
213 HgStatusCommand cmd = new HgStatusCommand(repo); 225 HgStatusCommand cmd = new HgStatusCommand(repo);
214 StatusCollector sc = new StatusCollector(); 226 StatusCollector sc = new StatusCollector();
215 cmd.all().base(7).execute(sc); 227 cmd.all().base(7).execute(sc);
228 assertTrue(sc.getErrors().isEmpty());
216 Path file5 = Path.create("dir/file5"); 229 Path file5 = Path.create("dir/file5");
217 // shall not be listed at all 230 // shall not be listed at all
218 assertTrue(sc.get(file5).isEmpty()); 231 assertTrue(sc.get(file5).isEmpty());
219 } 232 }
220 233
228 public void testTrackedModifiedIgnored() throws Exception { 241 public void testTrackedModifiedIgnored() throws Exception {
229 repo = Configuration.get().find("status-1"); 242 repo = Configuration.get().find("status-1");
230 HgStatusCommand cmd = new HgStatusCommand(repo); 243 HgStatusCommand cmd = new HgStatusCommand(repo);
231 StatusCollector sc = new StatusCollector(); 244 StatusCollector sc = new StatusCollector();
232 cmd.all().execute(sc); 245 cmd.all().execute(sc);
246 assertTrue(sc.getErrors().isEmpty());
233 final Path file2 = Path.create("file2"); 247 final Path file2 = Path.create("file2");
234 assertTrue(sc.get(file2).contains(Modified)); 248 assertTrue(sc.get(file2).contains(Modified));
235 assertTrue(sc.get(file2).size() == 1); 249 assertTrue(sc.get(file2).size() == 1);
236 } 250 }
237 251
244 public void testMarkedRemovedButStillInWC() throws Exception { 258 public void testMarkedRemovedButStillInWC() throws Exception {
245 repo = Configuration.get().find("status-1"); 259 repo = Configuration.get().find("status-1");
246 HgStatusCommand cmd = new HgStatusCommand(repo); 260 HgStatusCommand cmd = new HgStatusCommand(repo);
247 StatusCollector sc = new StatusCollector(); 261 StatusCollector sc = new StatusCollector();
248 cmd.all().execute(sc); 262 cmd.all().execute(sc);
263 assertTrue(sc.getErrors().isEmpty());
249 Path file4 = Path.create("dir/file4"); 264 Path file4 = Path.create("dir/file4");
250 assertTrue(sc.get(file4).contains(Removed)); 265 assertTrue(sc.get(file4).contains(Removed));
251 assertTrue(sc.get(file4).size() == 1); 266 assertTrue(sc.get(file4).size() == 1);
252 // 267 //
253 // different code path (collect != null) 268 // different code path (collect != null)
254 cmd.base(3).execute(sc = new StatusCollector()); 269 cmd.base(3).execute(sc = new StatusCollector());
270 assertTrue(sc.getErrors().isEmpty());
255 assertTrue(sc.get(file4).contains(Removed)); 271 assertTrue(sc.get(file4).contains(Removed));
256 assertTrue(sc.get(file4).size() == 1); 272 assertTrue(sc.get(file4).size() == 1);
257 // 273 //
258 // wasn't there in rev 2, shall not be reported at all 274 // wasn't there in rev 2, shall not be reported at all
259 cmd.base(2).execute(sc = new StatusCollector()); 275 cmd.base(2).execute(sc = new StatusCollector());
276 assertTrue(sc.getErrors().isEmpty());
260 assertTrue(sc.get(file4).isEmpty()); 277 assertTrue(sc.get(file4).isEmpty());
261 } 278 }
262 279
263 /* 280 /*
264 * status-1/dir/file3 tracked, listed in .hgignore since rev 4, removed (hg remove file3) from repo and WC 281 * status-1/dir/file3 tracked, listed in .hgignore since rev 4, removed (hg remove file3) from repo and WC
271 // check branch !known, ignored 288 // check branch !known, ignored
272 repo = Configuration.get().find("status-1"); 289 repo = Configuration.get().find("status-1");
273 HgStatusCommand cmd = new HgStatusCommand(repo); 290 HgStatusCommand cmd = new HgStatusCommand(repo);
274 StatusCollector sc = new StatusCollector(); 291 StatusCollector sc = new StatusCollector();
275 cmd.all().execute(sc); 292 cmd.all().execute(sc);
293 assertTrue(sc.getErrors().isEmpty());
276 final Path file3 = Path.create("dir/file3"); 294 final Path file3 = Path.create("dir/file3");
277 assertTrue(sc.get(file3).contains(Ignored)); 295 assertTrue(sc.get(file3).contains(Ignored));
278 assertTrue(sc.get(file3).size() == 1); 296 assertTrue(sc.get(file3).size() == 1);
279 // 297 //
280 cmd.base(3).execute(sc = new StatusCollector()); 298 cmd.base(3).execute(sc = new StatusCollector());
299 assertTrue(sc.getErrors().isEmpty());
281 assertTrue(sc.get(file3).contains(Ignored)); 300 assertTrue(sc.get(file3).contains(Ignored));
282 assertTrue(sc.get(file3).contains(Removed)); 301 assertTrue(sc.get(file3).contains(Removed));
283 assertTrue(sc.get(file3).size() == 2); 302 assertTrue(sc.get(file3).size() == 2);
284 // 303 //
285 cmd.base(5).execute(sc = new StatusCollector()); 304 cmd.base(5).execute(sc = new StatusCollector());
305 assertTrue(sc.getErrors().isEmpty());
286 assertTrue(sc.get(file3).contains(Ignored)); 306 assertTrue(sc.get(file3).contains(Ignored));
287 assertTrue(sc.get(file3).size() == 1); 307 assertTrue(sc.get(file3).size() == 1);
288 // 308 //
289 cmd.base(0).execute(sc = new StatusCollector()); 309 cmd.base(0).execute(sc = new StatusCollector());
310 assertTrue(sc.getErrors().isEmpty());
290 assertTrue(sc.get(file3).contains(Ignored)); 311 assertTrue(sc.get(file3).contains(Ignored));
291 assertTrue(sc.get(file3).size() == 1); 312 assertTrue(sc.get(file3).size() == 1);
292 313
293 } 314 }
294 315
302 repo = Configuration.get().find("status-1"); 323 repo = Configuration.get().find("status-1");
303 HgStatusCommand cmd = new HgStatusCommand(repo); 324 HgStatusCommand cmd = new HgStatusCommand(repo);
304 StatusCollector sc = new StatusCollector(); 325 StatusCollector sc = new StatusCollector();
305 cmd.base(1); 326 cmd.base(1);
306 cmd.all().execute(sc); 327 cmd.all().execute(sc);
328 assertTrue(sc.getErrors().isEmpty());
307 final Path file1 = Path.create("file1"); 329 final Path file1 = Path.create("file1");
308 assertTrue(sc.get(file1).contains(Unknown)); 330 assertTrue(sc.get(file1).contains(Unknown));
309 assertTrue(sc.get(file1).contains(Removed)); 331 assertTrue(sc.get(file1).contains(Removed));
310 assertTrue(sc.get(file1).size() == 2); 332 assertTrue(sc.get(file1).size() == 2);
311 // 333 //
312 // no file1 in rev 2, shall be reported as unknown only 334 // no file1 in rev 2, shall be reported as unknown only
313 cmd.base(2).execute(sc = new StatusCollector()); 335 cmd.base(2).execute(sc = new StatusCollector());
336 assertTrue(sc.getErrors().isEmpty());
314 assertTrue(sc.get(file1).contains(Unknown)); 337 assertTrue(sc.get(file1).contains(Unknown));
315 assertTrue(sc.get(file1).size() == 1); 338 assertTrue(sc.get(file1).size() == 1);
316 } 339 }
317 340
318 @Test 341 @Test
320 repo = Configuration.get().find("status-1"); 343 repo = Configuration.get().find("status-1");
321 HgStatusCommand cmd = new HgStatusCommand(repo); 344 HgStatusCommand cmd = new HgStatusCommand(repo);
322 StatusCollector sc = new StatusCollector(); 345 StatusCollector sc = new StatusCollector();
323 cmd.match(new PathGlobMatcher("*")); 346 cmd.match(new PathGlobMatcher("*"));
324 cmd.all().execute(sc); 347 cmd.all().execute(sc);
348 assertTrue(sc.getErrors().isEmpty());
325 /* 349 /*
326 * C .hgignore 350 * C .hgignore
327 * ? file1 351 * ? file1
328 * M file2 352 * M file2
329 * C readme 353 * C readme
334 assertTrue(sc.get(Removed).isEmpty()); 358 assertTrue(sc.get(Removed).isEmpty());
335 assertTrue(sc.get(Clean).size() == 2); 359 assertTrue(sc.get(Clean).size() == 2);
336 assertTrue(sc.get(Modified).size() == 1); 360 assertTrue(sc.get(Modified).size() == 1);
337 // 361 //
338 cmd.match(new PathGlobMatcher("dir/*")).execute(sc = new StatusCollector()); 362 cmd.match(new PathGlobMatcher("dir/*")).execute(sc = new StatusCollector());
363 assertTrue(sc.getErrors().isEmpty());
339 /* 364 /*
340 * I dir/file3 365 * I dir/file3
341 * R dir/file4 366 * R dir/file4
342 * R dir/file5 367 * R dir/file5
343 */ 368 */
412 HgStatusCommand cmd = new HgStatusCommand(repo); 437 HgStatusCommand cmd = new HgStatusCommand(repo);
413 cmd.base(3).revision(8).all(); 438 cmd.base(3).revision(8).all();
414 cmd.match(new PathGlobMatcher("dir/*")); 439 cmd.match(new PathGlobMatcher("dir/*"));
415 StatusCollector sc = new StatusCollector(); 440 StatusCollector sc = new StatusCollector();
416 cmd.execute(sc); 441 cmd.execute(sc);
442 assertTrue(sc.getErrors().isEmpty());
417 final Path file3 = Path.create("dir/file3"); 443 final Path file3 = Path.create("dir/file3");
418 final Path file4 = Path.create("dir/file4"); 444 final Path file4 = Path.create("dir/file4");
419 final Path file5 = Path.create("dir/file5"); 445 final Path file5 = Path.create("dir/file5");
420 // 446 //
421 assertTrue(sc.get(file3).contains(Removed)); 447 assertTrue(sc.get(file3).contains(Removed));
462 final long end = System.currentTimeMillis(); 488 final long end = System.currentTimeMillis();
463 System.out.printf("'hg status -A --rev 3:80', %d runs: Native client total %d (%d per run), Java client %d (%d)\n", runs, start2-start1, (start2-start1)/runs, end-start2, (end-start2)/runs); 489 System.out.printf("'hg status -A --rev 3:80', %d runs: Native client total %d (%d per run), Java client %d (%d)\n", runs, start2-start1, (start2-start1)/runs, end-start2, (end-start2)/runs);
464 } 490 }
465 491
466 private void report(String what, StatusCollector r) { 492 private void report(String what, StatusCollector r) {
493 assertTrue(r.getErrors().isEmpty());
467 reportNotEqual(what + "#MODIFIED", r.get(Modified), statusParser.getModified()); 494 reportNotEqual(what + "#MODIFIED", r.get(Modified), statusParser.getModified());
468 reportNotEqual(what + "#ADDED", r.get(Added), statusParser.getAdded()); 495 reportNotEqual(what + "#ADDED", r.get(Added), statusParser.getAdded());
469 reportNotEqual(what + "#REMOVED", r.get(Removed), statusParser.getRemoved()); 496 reportNotEqual(what + "#REMOVED", r.get(Removed), statusParser.getRemoved());
470 reportNotEqual(what + "#CLEAN", r.get(Clean), statusParser.getClean()); 497 reportNotEqual(what + "#CLEAN", r.get(Clean), statusParser.getClean());
471 reportNotEqual(what + "#IGNORED", r.get(Ignored), statusParser.getIgnored()); 498 reportNotEqual(what + "#IGNORED", r.get(Ignored), statusParser.getIgnored());