diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-14 16:04:09 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-03-14 16:04:09 -0400 |
commit | a31a9b7ef81f47d6972a2a74fe9c660c4d250d55 (patch) | |
tree | bf83edd8e11690892dae92b5109d49f8e265bcf9 /src/utils.c | |
parent | 0c21db396d0ca18db4326c53fe69137a69e3f4cb (diff) | |
download | stasis-a31a9b7ef81f47d6972a2a74fe9c660c4d250d55.tar.gz |
Add fix_tox_conf function
* If the user calls tox in a test script like so: tox {{ workaround.tox_posargs }}, then a temporary tox configuration will be generated and the appropriate arguments to use it will be injected into tox's command line arguments
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/src/utils.c b/src/utils.c index 047a266..85a3b56 100644 --- a/src/utils.c +++ b/src/utils.c @@ -590,4 +590,60 @@ int xml_pretty_print_in_place(const char *filename, const char *pretty_print_pro guard_free(tempfile); guard_free(result); return -1; -}
\ No newline at end of file +} + +int fix_tox_conf(const char *filename, char **result) { + struct INIFILE *toxini; + FILE *fptemp; + char *tempfile; + const char *with_posargs = " \\\n {posargs}\n"; + + tempfile = xmkstemp(&fptemp, "w+"); + if (!tempfile) { + return -1; + } + + if (!*result) { + *result = calloc(PATH_MAX, sizeof(*result)); + if (!*result) { + return -1; + } + } + + toxini = ini_open(filename); + if (!toxini) { + if (fptemp) { + guard_free(tempfile); + fclose(fptemp); + } + guard_free(*result); + return -1; + } + + for (size_t i = 0; i < toxini->section_count; i++) { + struct INISection *section = toxini->section[i]; + if (section) { + if (startswith(section->key, "testenv")) { + for (size_t k = 0; k < section->data_count; k++) { + struct INIData *data = section->data[k]; + if (data) { + if (!strcmp(data->key, "commands") && (startswith(data->value, "pytest") && !strstr(data->value, "{posargs}"))) { + strip(data->value); + data->value = realloc(data->value, strlen(data->value) + strlen(with_posargs) + 1); + strcat(data->value, with_posargs); + } + } + } + } + } + } + + if (ini_write(toxini, &fptemp)) { + guard_free(tempfile); + } + fclose(fptemp); + + *result = tempfile; + ini_free(&toxini); + return 0; +} |