Skip to content

Commit

Permalink
Begin implementing sync and reccomend, and fix some mem leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
chinarjoshi committed Sep 21, 2023
1 parent 9740fd0 commit d53f639
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ sources = [
'src/subcmds/remove.c',
'src/subcmds/edit.c',
'src/subcmds/show.c',
'src/subcmds/sync.c',
'src/subcmds/reccomend.c',
'src/subcmds/helpers.c',
'src/toml/toml.c',
'src/toml/toml_extensions.c',
Expand All @@ -34,6 +36,8 @@ test_sources = [
'src/subcmds/remove.c',
'src/subcmds/edit.c',
'src/subcmds/show.c',
'src/subcmds/sync.c',
'src/subcmds/reccomend.c',
'src/subcmds/helpers.c',
'src/toml/toml.c',
'src/toml/toml_extensions.c',
Expand Down
9 changes: 9 additions & 0 deletions src/parse_args/parse_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ int show_parse_opt(int key, char *arg, struct argp_state *state) {
case 'l':
show->local = true;
break;
case 'c':
if (show->commit_hash) {
printf("Error (invalid args): multiple commit hashes provided.\n");
return 1;
}
if (!(show->commit_hash = malloc(strlen(arg) + 1)))
return 1;
strcpy(show->commit_hash, arg);
break;
case ARGP_KEY_ARG:;
AliasListNode *new_node;
if (!(new_node = malloc(sizeof(AliasListNode))))
Expand Down
7 changes: 7 additions & 0 deletions src/subcmds/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ int cleanup(const char *message, const char *message_arg,
return 0;
}

void free_env_paths(char **paths, int num_paths) {
for (int i = 0; i < num_paths; ++i) {
free(paths[i]);
}
free(paths);
}

// Sets 'ALIAS_FNAME' to the value of environmental variable "ACRONYM_ALIAS_FILE",
// or if not found, the expansion of "~/.aliases"
// Sets 'AUTOENV_FNAME' to its env variable, or '.env' if not found
Expand Down
3 changes: 3 additions & 0 deletions src/subcmds/reccomend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "subcmds.h"

bool reccomend_cmd(Cli *cli) { return true; }
2 changes: 2 additions & 0 deletions src/subcmds/show.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ bool show_cmd(Cli *cli) {
filter_hash_table(ht, s.aliases, s.section);

printf("%s", ht_to_toml_str(ht));
free_hash_table(ht);
free_env_paths(env_paths, num_paths);
return true;
}
6 changes: 4 additions & 2 deletions src/subcmds/subcmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ extern bool (*sub_cmds[])(Cli *);

bool add_cmd(Cli *cli);
bool remove_cmd(Cli *cli);
bool tree_cmd(Cli *cli);
bool show_cmd(Cli *cli);
bool edit_cmd(Cli *cli);
bool show_cmd(Cli *cli);
bool sync_cmd(Cli *cli);
bool reccomend_cmd(Cli *cli);
bool is_valid_dir(const char *dir);
void setup_fname_buffers();
char **get_env_paths(const char *start, int *num_paths);
enum PathCmp compare_paths(const char *env_fname, const char *directory);
int cleanup(const char *message, const char *message_arg,
HashTable *ht, FILE *f, const char *fname_to_remove);
void free_env_paths(char **paths, int num_paths);
void filter_hash_table(HashTable *ht, AliasListNode *l, bool section);

#endif
3 changes: 3 additions & 0 deletions src/subcmds/sync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "subcmds.h"

bool sync_cmd(Cli *cli) { return true; }
66 changes: 64 additions & 2 deletions tests/test_subcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ START_TEST(test_remove_cmd_normal) {
END_TEST

START_TEST(test_show_cmd_normal) {
const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
Cli cli = {
.type = SHOW,
.verbosity = 1,
};

const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
ck_assert(show_cmd(&cli));
}
END_TEST
Expand All @@ -133,6 +133,58 @@ START_TEST(test_show_cmd_local) {
}
END_TEST

START_TEST(test_show_cmd_aliases) {
const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
AliasListNode b = { .data = "gp", .next = NULL };
AliasListNode a = { .data = "a", .next = &b };
Cli cli = {
.type = SHOW,
.verbosity = 1,
.cmd.show.aliases = &a
};

ck_assert(show_cmd(&cli));
}
END_TEST

START_TEST(test_show_cmd_commit_hash) {
const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
Cli cli = {
.type = SHOW,
.verbosity = 1,
.cmd.show.commit_hash = "724976b"
};

ck_assert(show_cmd(&cli));
}
END_TEST

START_TEST(test_sync_cmd_normal) {
const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
Cli cli = {
.type = SYNC,
.verbosity = 1,
};

ck_assert(sync_cmd(&cli));
}
END_TEST

START_TEST(test_reccomend_cmd_normal) {
const char *path = "/home/c/.aliases";
strncpy(ALIAS_FNAME, path, strlen(path));
Cli cli = {
.type = RECCOMEND,
.verbosity = 1,
};

ck_assert(reccomend_cmd(&cli));
}
END_TEST

START_TEST(test_compare_paths) {
char *env_file = "/home/c/projects/.env";
char *path1 = "/home/c/projects";
Expand Down Expand Up @@ -163,9 +215,19 @@ Suite *subcmds_suite(void) {

TCase *tc_show = tcase_create("Show cmd");
tcase_add_test(tc_show, test_show_cmd_normal);
tcase_add_test(tc_show, test_show_cmd_aliases);
tcase_add_test(tc_show, test_show_cmd_commit_hash);
tcase_add_test(tc_show, test_show_cmd_local);
suite_add_tcase(s, tc_show);

TCase *tc_sync = tcase_create("Sync cmd");
tcase_add_test(tc_sync, test_sync_cmd_normal);
suite_add_tcase(s, tc_sync);

TCase *tc_reccomend = tcase_create("Reccomend cmd");
tcase_add_test(tc_reccomend, test_reccomend_cmd_normal);
suite_add_tcase(s, tc_reccomend);

TCase *tc_helpers = tcase_create("Subcmd helpers");
tcase_add_test(tc_helpers, test_compare_paths);
suite_add_tcase(s, tc_helpers);
Expand Down

0 comments on commit d53f639

Please sign in to comment.