void simple_log_test(void)
{
    struct commit* commit_list = NULL;
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);
    FILE* asdf = fopen("asdf.txt", "w");
    fclose(asdf);
    retval = beargit_add("asdf.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "GO BEARS!1");
    run_commit(&commit_list, "GO BEARS!2");
    run_commit(&commit_list, "GO BEARS!3");

    retval = beargit_log(INT_MAX);
    CU_ASSERT(0==retval);

    struct commit* cur_commit = commit_list;

    const int LINE_SIZE = 512;
    char line[LINE_SIZE];

    FILE* fstdout = fopen("TEST_STDOUT", "r");
    CU_ASSERT_PTR_NOT_NULL(fstdout);

    while (cur_commit != NULL) {
      char refline[LINE_SIZE];

      // First line is empty
      CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
      CU_ASSERT(!strcmp(line,"\n"));

      // Second line is commit -- don't check the ID.
      CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
      CU_ASSERT(!strncmp(line,"commit", strlen("commit")));

      // Third line is msg
      sprintf(refline, "    %s\n", cur_commit->msg);
      CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
      CU_ASSERT_STRING_EQUAL(line, refline);

      cur_commit = cur_commit->next;
    }

    // Last line is empty
    CU_ASSERT_PTR_NOT_NULL(fgets(line, LINE_SIZE, fstdout));
    CU_ASSERT(!strcmp(line,"\n"));

    CU_ASSERT_PTR_NULL(fgets(line, LINE_SIZE, fstdout));

    // It's the end of output
    CU_ASSERT(feof(fstdout));
    fclose(fstdout);

    free_commit_list(&commit_list);
}
//tests the add command
void add_test(void)
{
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);

    retval = beargit_add("asdf.txt"); //checks that a normal add will not error
    CU_ASSERT(0==retval);

    retval = beargit_add("asdf.txt"); //check that add will error if the file has already been added
    CU_ASSERT(3==retval);

    FILE* findex = fopen(".beargit/index", "r");
    char line[512];
    fgets(line, sizeof(line), findex);
    CU_ASSERT(line == "asdf.txt"); //check add properly added filename to index
    CU_ASSERT(fgets(line, sizeof(line), findex) == NULL); //check that second add didn't still add to index

    fclose(findex);
}
Esempio n. 3
0
/* Simple test of fread().
 * Reads the data previously written by testFPRINTF()
 * and checks whether the expected characters are present.
 * Must be run after testFPRINTF().
 */
void simple_sample_test(void)
{
    // This is a very basic test. Your tests should likely do more than this.
    // We suggest checking the outputs of printfs/fprintfs to both stdout
    // and stderr. To make this convenient for you, the tester replaces
    // printf and fprintf with copies that write data to a file for you
    // to access. To access all output written to stdout, you can read
    // from the "TEST_STDOUT" file. To access all output written to stderr,
    // you can read from the "TEST_STDERR" file.
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);
    retval = beargit_add("asdf.txt");
    CU_ASSERT(0==retval);
}
//tests the remove command
void rm_test(void)
{
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);

    retval = beargit_add("asdf.txt");
    CU_ASSERT(0==retval);

    retval = beargit_rm("asdf.txt"); //tests a normal remove will not error
    CU_ASSERT(0==retval);

    retval = beargit_rm("asdf.txt"); //tests that a remove on a nonexistent/previously removed file does error
    CU_ASSERT(1==retval);

    FILE* findex = fopen(".beargit/index", "r");
    char line[512];
    fgets(line, sizeof(line), findex);
    CU_ASSERT(line != "asdf.txt"); //check that a removed file is removed from the index
  
    fclose(findex);
}
Esempio n. 5
0
//This suite tests merging multiple files. It first tests for error cases, then
//it tests for non-conflicting files then test for conflicted files.
void merge_test(void) {

    struct commit* commit_list = NULL;
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);

    write_string_to_file("wug.txt", "Dis a wug");
    write_string_to_file("wug1.txt", "Dis a wug 1.5");
    write_string_to_file("wug2.txt", "Der two wugz");
    write_string_to_file("wug3.txt", "Dis not a wug");

    retval = beargit_add("wug.txt");
    CU_ASSERT(0==retval);
    retval = beargit_add("wug1.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!1");

    //save commit1's ID
    char commit1_id[512] = "";
    read_string_from_file(".beargit/.prev", commit1_id, 512);

    retval = beargit_add("wug2.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!2");

    //remove wug, wug1 and wug2
    fs_rm("wug.txt");
    retval = beargit_rm("wug.txt");
    CU_ASSERT(0==retval);

    fs_rm("wug1.txt");
    retval = beargit_rm("wug1.txt");
    CU_ASSERT(0==retval);

    fs_rm("wug2.txt");
    retval = beargit_rm("wug2.txt");
    CU_ASSERT(0==retval);

    //add wug3 & commit
    retval = beargit_add("wug3.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!3");

    //Check for err msg when merging non existent commit ID / brench
    retval = beargit_merge("alskdfjlaskfj");
    CU_ASSERT(1==retval); //first line of stderr: "ERROR:  No branch or commit alskdfjlaskfj exists."

    //test merging non-conflicted files
    retval = beargit_merge(commit1_id);
    CU_ASSERT(0==retval); //first line of stdout: "wug.txt added\nwug1.txt added\n"
    //check files actually moved
    FILE* fstdout_wug = fopen("wug.txt", "r");
    CU_ASSERT_PTR_NOT_NULL(fstdout_wug);
    FILE* fstdout_wug1 = fopen("wug1.txt", "r");
    CU_ASSERT_PTR_NOT_NULL(fstdout_wug1);
    fclose(fstdout_wug);
    fclose(fstdout_wug1);


    //test merging conflicted files
    retval = beargit_merge(commit1_id);
    CU_ASSERT(0==retval); //first line of stdout: "wug.txt conflicted copy created \nwug1.txt conflicted copy created\n"
    //check conflicted files created
    char wugc[512] = "wug.txt.";
    strcat(wugc, commit1_id);
    char wugc1[512] = "wug1.txt.";
    strcat(wugc1, commit1_id);

    FILE* fstdout_wugc = fopen(wugc, "r");
    CU_ASSERT_PTR_NOT_NULL(fstdout_wugc);
    FILE* fstdout_wugc1 = fopen(wugc1, "r");
    CU_ASSERT_PTR_NOT_NULL(fstdout_wugc1);
    fclose(fstdout_wugc);
    fclose(fstdout_wugc1);

    //Read stderr file
    char PostToBe4Err[512] = "ERROR:  No branch or commit alskdfjlaskfj exists.\n";

    char ActualErr[512] ="";
    read_string_from_file("TEST_STDERR", ActualErr, 512);
    CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0);

    // read stdout
    char PostToBe[512] = "wug.txt added\nwug1.txt added\nwug.txt conflicted copy created\nwug1.txt conflicted copy created\n";

    char Actual[512] ="";
    read_string_from_file("TEST_STDOUT", Actual, 512);
    CU_ASSERT(strcmp(PostToBe, Actual) == 0);


    free_commit_list(&commit_list);
}
Esempio n. 6
0
//this test tests reset. It checks for the two erroring cases
//It checks if reset replaces the version of the file in the
//working directory with the specified file for reset.
//it also tests if index is update and file is sucessfully
//reset if the file is not in the working directory.
void reset_test(void) {
    struct commit* commit_list = NULL;
    int retval;
    retval = beargit_init();
    CU_ASSERT(0==retval);

    write_string_to_file("wug.txt", "Dis a wug");
    write_string_to_file("wug2.txt", "Der two wugz");
    write_string_to_file("wug3.txt", "Dis not a wug");

    retval = beargit_add("wug.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!1");

    retval = beargit_add("wug2.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!2");

    retval = beargit_add("wug3.txt");
    CU_ASSERT(0==retval);
    run_commit(&commit_list, "THIS IS BEAR TERRITORY!3");

    char commit_id[512] = "";
    read_string_from_file(".beargit/.prev", commit_id, 512);

    //Check for error if commit doens't exit
    retval = beargit_reset("alskdfjlaskfj", "wug.txt");
    CU_ASSERT(1==retval); //first line of stderr: "ERROR:  Commit alskdfjlaskfj does not exist.\n"

    //Check for error if file not in commit
    retval = beargit_reset(commit_id, "wug6.txt");
    CU_ASSERT(1==retval); //second line of stderr: "ERROR:  wug6.txt is not in the index of commit [insert commit_id].\n"

    //modifies wug.txt in working directory
    write_string_to_file("wug.txt", "JK dis not a wug");

    //Resets wug.txt from commit 3
    retval = beargit_reset(commit_id, "wug.txt");
    CU_ASSERT(0==retval);
    char stuffinwug[512] = "";
    read_string_from_file("wug.txt", stuffinwug, 512);
    CU_ASSERT(strcmp(stuffinwug, "Dis a wug") == 0);

    //remove wug3.txt
    fs_rm("wug3.txt");
    retval = beargit_rm("wug3.txt");
    CU_ASSERT(retval == 0);
    FILE* fstdout_wug = fopen("wug3.txt", "r");
    CU_ASSERT_PTR_NULL(fstdout_wug);


    //try to reset wug3.txt
    retval = beargit_reset(commit_id, "wug3.txt");
    FILE* check_wug_exist = fopen("wug3.txt", "r");
    CU_ASSERT_PTR_NOT_NULL(check_wug_exist);
    char check_index_4wug[512] = "";
    read_string_from_file(".beargit/.index", check_index_4wug, 512);
    char PostToBe[512] = "wug2.txt\nwug.txt\nwug3.txt\n";
    CU_ASSERT(strcmp(check_index_4wug, PostToBe) == 0);
    fclose(check_wug_exist);

    //Read stderr file

    char PostToBe4Err[512] = "";
    strcat(PostToBe4Err, "ERROR:  Commit alskdfjlaskfj does not exist.\nERROR:  wug6.txt is not in the index of commit ");
    strcat(PostToBe4Err, commit_id);
    strcat(PostToBe4Err, ".\n");

    char ActualErr[512] ="";
    read_string_from_file("TEST_STDERR", ActualErr, 512);
    CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0);

    free_commit_list(&commit_list);
}
Esempio n. 7
0
int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <command> [<args>]\n", argv[0]);
        return 2;
    }

    // TODO: If students aren't going to write this themselves, replace by clean
    // implementation using function pointers.
    if (strcmp(argv[1], "init") == 0) {

      if (check_initialized()) {
        fprintf(stderr, "ERROR: Repository is already initialized\n");
        return 1;
      }

      return beargit_init();

    } else {

        if (!check_initialized()) {
            fprintf(stderr, "ERROR: Repository is not initialized\n");
            return 1;
        }

        if (strcmp(argv[1], "add") == 0 || strcmp(argv[1], "rm") == 0) {

          if (argc < 3 || !check_filename(argv[2])) {
            fprintf(stderr, "ERROR: No or invalid filename given\n");
            return 1;
          }

          if (strcmp(argv[1], "rm") == 0) {
            return beargit_rm(argv[2]);
          } else {
            return beargit_add(argv[2]);
          }

        } else if (strcmp(argv[1], "commit") == 0) {

          if (argc < 4 || strcmp(argv[2], "-m") != 0) {
            fprintf(stderr, "ERROR: Need a commit message (-m <msg>)\n");
            return 1;
          }

          if (strlen(argv[3]) > MSG_SIZE-1) {
            fprintf(stderr, "ERROR: Message is too long!\n");
            return 1;
          }

          return beargit_commit(argv[3]);

        } else if (strcmp(argv[1], "status") == 0) {
            return beargit_status();
        } else if (strcmp(argv[1], "log") == 0) {
            return beargit_log();
        } else if (strcmp(argv[1], "branch") == 0) {
            return beargit_branch();
        } else if (strcmp(argv[1], "checkout") == 0) {
            int branch_new = 0;
            char* arg = NULL;

            for (int i = 2; i < argc; i++) {
              if (argv[i][0] == '-') {
                if (strcmp(argv[i], "-b") == 0) {
                  branch_new = 1;
                  continue;
                } else {
                  fprintf(stderr, "ERROR: Invalid argument: %s", argv[i]);
                  return 1;
                }
              }

              if (arg) {
                  fprintf(stderr, "ERROR: Too many arguments for checkout!");
                  return 1;
              }

              arg = argv[i];
            }

            return beargit_checkout(arg, branch_new);
        } else {
            fprintf(stderr, "ERROR: Unknown command \"%s\"\n", argv[1]);
            return 1;
        }
    }
}