Changeset 2049


Ignore:
Timestamp:
Sep 4, 2009, 10:58:29 PM (14 years ago)
Author:
Peter
Message:

using strerror in error msg from OptionFile? (fixes #559)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/commandline_test.cc

    r1998 r2049  
    4242bool test_file(yat::test::Suite& error);
    4343bool test_file2(yat::test::Suite& error);
     44bool test_file3(yat::test::Suite& error);
     45bool test_file3_(yat::test::Suite& error, const std::string&);
     46bool test_file4(yat::test::Suite& error);
     47bool test_file4_(yat::test::Suite& error, const std::string&);
    4448bool test_failures(yat::test::Suite& error);
    4549bool test_option_name_clash(yat::test::Suite& suite);
     
    5256
    5357  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  }
    5476
    5577  try {
     
    5981    suite.add(test_file(suite));
    6082    suite.add(test_file2(suite));
     83    suite.add(test_file3(suite));
     84    suite.add(test_file4(suite));
    6185    suite.add(test_failures(suite));
    6286    suite.add(test_option_name_clash(suite));
     
    498522}
    499523
     524
    500525bool test_file2(yat::test::Suite& suite)
    501526{
     
    508533  }
    509534
    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");
    516536  if (!subdir.exists()) {
    517537    if (mkdir(subdir.path().c_str(), 0555)) {
     
    526546    }
    527547
    528   FileUtil file("testSubDir/commandline_test.dir/out.txt");
     548  FileUtil file("testSubDir/commandline_test.dir/write-protected/out.txt");
    529549  if (file.exists()) {
    530550    suite.err() << "FileUtil::exists returns true unexpectedly\n";
     
    545565    int ac = 3;
    546566    char* av[] = { "test_prog", "--out",
    547                    "testSubDir/commandline_test.dir/out.txt"};
     567                   "testSubDir/commandline_test.dir/write-protected/out.txt"};
    548568    try {
    549569      cmd.parse(ac,av);
     
    558578  return ok;
    559579}
     580
     581
     582bool 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
     595bool 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
     616bool 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
     634bool 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
    560655
    561656bool test_option_name_clash(yat::test::Suite& suite)
     
    587682  return ok;
    588683}
     684
    589685
    590686bool test_free_arg(yat::test::Suite& suite)
  • trunk/yat/utility/OptionFile.cc

    r1954 r2049  
    6969        if (*iter=='r' && fu.permissions("r")){
    7070          std::stringstream ss;
    71           ss << "cannot open `" << value() << "' for reading: Permission denied";
     71          ss << "cannot open `" << value() << "' for reading: "
     72             << strerror(errno);
    7273          throw cmd_error(ss.str());
    7374        }
     
    7576          std::stringstream ss;
    7677          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);
    8479          throw cmd_error(ss.str());
    8580        }
    8681      }
    8782      std::stringstream ss;
    88       ss << value() << ": Permission denied";
     83      ss << "`" << value() << "': " << strerror(errno);
    8984      throw cmd_error(ss.str());
    9085    }
Note: See TracChangeset for help on using the changeset viewer.