Changeset 2049
- Timestamp:
- Sep 4, 2009, 10:58:29 PM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/commandline_test.cc
r1998 r2049 42 42 bool test_file(yat::test::Suite& error); 43 43 bool test_file2(yat::test::Suite& error); 44 bool test_file3(yat::test::Suite& error); 45 bool test_file3_(yat::test::Suite& error, const std::string&); 46 bool test_file4(yat::test::Suite& error); 47 bool test_file4_(yat::test::Suite& error, const std::string&); 44 48 bool test_failures(yat::test::Suite& error); 45 49 bool test_option_name_clash(yat::test::Suite& suite); … … 52 56 53 57 suite.err() << "testing commandline" << std::endl; 58 59 // Peter, creation of this directory should perhaps be in a more central place 60 FileUtil dir("testSubDir"); 61 if (!dir.exists()) 62 if (mkdir(dir.path().c_str(), 0755)) { 63 suite.err() << "mkdir " << dir.path() << " failed\n"; 64 return 1; 65 } 66 FileUtil subdir("testSubDir/commandline_test.dir"); 67 if (!subdir.exists()) { 68 if (mkdir(subdir.path().c_str(), 0755)) { 69 suite.err() << "mkdir " << subdir.path() << " failed\n"; 70 return 1; 71 } 72 } 73 if (subdir.permissions("w")) { 74 chmod(subdir.path().c_str(), S_IREAD | S_IWRITE | S_IEXEC); 75 } 54 76 55 77 try { … … 59 81 suite.add(test_file(suite)); 60 82 suite.add(test_file2(suite)); 83 suite.add(test_file3(suite)); 84 suite.add(test_file4(suite)); 61 85 suite.add(test_failures(suite)); 62 86 suite.add(test_option_name_clash(suite)); … … 498 522 } 499 523 524 500 525 bool test_file2(yat::test::Suite& suite) 501 526 { … … 508 533 } 509 534 510 // Peter, creation of this directory should perhaps be in a more central place 511 FileUtil dir("testSubDir"); 512 if (!dir.exists()) 513 mkdir(dir.path().c_str(), 0755); 514 515 FileUtil subdir("testSubDir/commandline_test.dir"); 535 FileUtil subdir("testSubDir/commandline_test.dir/write-protected"); 516 536 if (!subdir.exists()) { 517 537 if (mkdir(subdir.path().c_str(), 0555)) { … … 526 546 } 527 547 528 FileUtil file("testSubDir/commandline_test.dir/ out.txt");548 FileUtil file("testSubDir/commandline_test.dir/write-protected/out.txt"); 529 549 if (file.exists()) { 530 550 suite.err() << "FileUtil::exists returns true unexpectedly\n"; … … 545 565 int ac = 3; 546 566 char* av[] = { "test_prog", "--out", 547 "testSubDir/commandline_test.dir/ out.txt"};567 "testSubDir/commandline_test.dir/write-protected/out.txt"}; 548 568 try { 549 569 cmd.parse(ac,av); … … 558 578 return ok; 559 579 } 580 581 582 bool test_file3(yat::test::Suite& suite) 583 { 584 suite.err() << "Testing OptionFile in non-existing tree\n"; 585 // testing error message from OptionFile when tree is not existing 586 bool ok=true; 587 ok &= test_file3_(suite, "r"); 588 ok &= test_file3_(suite, "w"); 589 ok &= test_file3_(suite, "x"); 590 ok &= test_file3_(suite, "d"); 591 return ok; 592 } 593 594 595 bool test_file3_(yat::test::Suite& suite, const std::string& perm) 596 { 597 CommandLine cmd; 598 OptionFile file(cmd, "file", "", true, false, perm); 599 600 suite.err() << "Testing OptionFile '" << perm << "' ... "; 601 try { 602 int ac = 3; 603 char* av[] = { "test_prog", "--file", "sjhgaw/jmegb/tmp.tmpx"}; 604 cmd.parse(ac,av); 605 suite.err() << "no\n"; 606 return false; 607 } 608 catch (cmd_error& e) { 609 suite.err() << "ok\n"; 610 suite.err() << "catch expected error: " << e.what() << "\n"; 611 } 612 return true; 613 } 614 615 616 bool test_file4(yat::test::Suite& suite) 617 { 618 FileUtil file("testSubDir/commandline_test.dir/test_file4.txt"); 619 if (!file.exists()) { 620 std::ofstream os(file.path().c_str()); 621 } 622 chmod(file.path().c_str(), 0); 623 624 suite.err() << "Testing OptionFile with no permssions\n"; 625 bool ok=true; 626 ok &= test_file4_(suite, "r"); 627 ok &= test_file4_(suite, "w"); 628 ok &= test_file4_(suite, "x"); 629 ok &= test_file4_(suite, "d"); 630 return ok; 631 } 632 633 634 bool test_file4_(yat::test::Suite& suite, const std::string& perm) 635 { 636 CommandLine cmd; 637 OptionFile file(cmd, "file", "", true, false, perm); 638 639 suite.err() << "Testing OptionFile '" << perm << "' ... "; 640 try { 641 int ac = 3; 642 char* av[] = { "test_prog", "--file", 643 "testSubDir/commandline_test.dir/test_file4.txt"}; 644 cmd.parse(ac,av); 645 suite.err() << "no\n"; 646 return false; 647 } 648 catch (cmd_error& e) { 649 suite.err() << "ok\n"; 650 suite.err() << "catch expected error: " << e.what() << "\n"; 651 } 652 return true; 653 } 654 560 655 561 656 bool test_option_name_clash(yat::test::Suite& suite) … … 587 682 return ok; 588 683 } 684 589 685 590 686 bool test_free_arg(yat::test::Suite& suite) -
trunk/yat/utility/OptionFile.cc
r1954 r2049 69 69 if (*iter=='r' && fu.permissions("r")){ 70 70 std::stringstream ss; 71 ss << "cannot open `" << value() << "' for reading: Permission denied"; 71 ss << "cannot open `" << value() << "' for reading: " 72 << strerror(errno); 72 73 throw cmd_error(ss.str()); 73 74 } … … 75 76 std::stringstream ss; 76 77 ss << "cannot create file `" << value() 77 << "': Permission denied"; 78 throw cmd_error(ss.str()); 79 } 80 else if (*iter=='d' && fu.permissions("d")){ 81 std::stringstream ss; 82 ss << value() 83 << "': Not a directory"; 78 << "': " << strerror(errno); 84 79 throw cmd_error(ss.str()); 85 80 } 86 81 } 87 82 std::stringstream ss; 88 ss << value() << ": Permission denied";83 ss << "`" << value() << "': " << strerror(errno); 89 84 throw cmd_error(ss.str()); 90 85 }
Note: See TracChangeset
for help on using the changeset viewer.