Skip to content

Commit

Permalink
Re-use temporary INI file path in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoellendorf committed Apr 21, 2024
1 parent c7b5df8 commit f8bcd90
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions test/test_iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
#define TEST_TXT_PATH "ressources/test.txt"
#define GRUEZI_INI_PATH "ressources/gruezi.ini"
#define UTF8_INI_PATH "ressources/utf8.ini"
#define MISFORMED_INI_PATH "ressources/misformed.ini"
#define TMP_INI_PATH "ressources/tmp.ini"
#define MISFORMED_INI_SEC0 "[12345"
#define MISFORMED_INI_SEC1 "12345]"
#define MISFORMED_INI_SEC2 "123]45"
#define MISFORMED_INI_ATTR "1111"
#define TMP_INI_PATH "ressources/tmp.ini"
#define QUOTES_INI_PATH "ressources/quotes.ini"
#define QUOTES_INI_SEC "quotes"
#define QUOTES_INI_ATTR0 "string0"
Expand Down Expand Up @@ -1060,137 +1059,137 @@ void Test_iniparser_misformed(CuTest *tc)
FILE *ini;
int ret;

create_empty_ini_file(MISFORMED_INI_PATH);
dic = iniparser_load(MISFORMED_INI_PATH);
create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC0, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC0,
MISFORMED_INI_PATH);
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(MISFORMED_INI_PATH, "w+");
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(MISFORMED_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
goto rm_ini;
}

CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1));
dictionary_del(dic);
ret = remove(MISFORMED_INI_PATH);
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}

create_empty_ini_file(MISFORMED_INI_PATH);
dic = iniparser_load(MISFORMED_INI_PATH);
create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC1, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC1,
MISFORMED_INI_PATH);
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(MISFORMED_INI_PATH, "w+");
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(MISFORMED_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1));
dictionary_del(dic);
ret = remove(MISFORMED_INI_PATH);
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}

create_empty_ini_file(MISFORMED_INI_PATH);
dic = iniparser_load(MISFORMED_INI_PATH);
create_empty_ini_file(TMP_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

ret = iniparser_set(dic, MISFORMED_INI_SEC2, NULL);

if (ret < 0) {
fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC2,
MISFORMED_INI_PATH);
TMP_INI_PATH);
goto del_dic;
}

iniparser_set(dic, MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, "2222");
/* test dictionary */
CuAssertIntEquals(tc, 2222, iniparser_getint(dic,
MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1));
ini = fopen(MISFORMED_INI_PATH, "w+");
ini = fopen(TMP_INI_PATH, "w+");

if (!ini) {
fprintf(stderr, "iniparser: cannot open %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH);
goto del_dic;
}

iniparser_dump_ini(dic, ini);
fclose(ini);
dictionary_del(dic);
/* check if section has been written as expected */
dic = iniparser_load(MISFORMED_INI_PATH);
dic = iniparser_load(TMP_INI_PATH);

if (!dic) {
fprintf(stderr, "cannot parse file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH);
return;
}

Expand All @@ -1199,10 +1198,10 @@ void Test_iniparser_misformed(CuTest *tc)
del_dic:
dictionary_del(dic);
rm_ini:
ret = remove(MISFORMED_INI_PATH);
ret = remove(TMP_INI_PATH);

if (ret) {
fprintf(stderr, "cannot remove file: %s\n", MISFORMED_INI_PATH);
fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH);
return;
}
}
Expand Down

0 comments on commit f8bcd90

Please sign in to comment.