Skip to content

Commit

Permalink
Add fix_tox_conf function
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jhunkeler committed Mar 14, 2024
1 parent 0c21db3 commit a31a9b7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/omc.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct OMC_GLOBAL {
char *tmpdir; //!< Path to temporary storage directory
char *conda_install_prefix; //!< Path to install conda
char *sysconfdir; //!< Path where OMC reads its configuration files (mission directory, etc)
struct {
char *tox_posargs;
} workaround;
struct Jfrog {
char *jfrog_artifactory_base_url;
char *jfrog_artifactory_product;
Expand Down
9 changes: 9 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,13 @@ int isempty_dir(const char *path);
* @return 0 on success, -1 on error
*/
int xml_pretty_print_in_place(const char *filename, const char *pretty_print_prog, const char *pretty_print_args);

/**
* Applies OMC fixups to a tox ini config
* @param filename path to tox.ini
* @param result path to processed configuration
* @return 0 on success, -1 on error
*/
int fix_tox_conf(const char *filename, char **result);

#endif //OMC_UTILS_H
19 changes: 19 additions & 0 deletions src/deliverable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,20 @@ void delivery_tests_run(struct Delivery *ctx) {
msg(OMC_MSG_L3, "Testing %s\n", ctx->tests[i].name);
memset(&proc, 0, sizeof(proc));

// Apply workaround for tox positional arguments
char *toxconf = NULL;
if (!access("tox.ini", F_OK)) {
msg(OMC_MSG_L3, "Fixing tox positional arguments\n");

fix_tox_conf("tox.ini", &toxconf);
if (!globals.workaround.tox_posargs) {
globals.workaround.tox_posargs = calloc(PATH_MAX, sizeof(*globals.workaround.tox_posargs));
} else {
memset(globals.workaround.tox_posargs, 0, PATH_MAX);
}
snprintf(globals.workaround.tox_posargs, PATH_MAX - 1, "-c %s --root .", toxconf);
}

// enable trace mode before executing each test script
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "set -x ; %s", ctx->tests[i].script);
Expand All @@ -1410,6 +1424,11 @@ void delivery_tests_run(struct Delivery *ctx) {
msg(OMC_MSG_ERROR, "Script failure: %s\n%s\n\nExit code: %d\n", ctx->tests[i].name, ctx->tests[i].script, status);
COE_CHECK_ABORT(!globals.continue_on_error, "Test failure")
}

if (toxconf) {
remove(toxconf);
guard_free(toxconf);
}
popd();
#else
msg(OMC_MSG_WARNING | OMC_MSG_L3, "TESTING DISABLED BY CODE!\n");
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ int main(int argc, char *argv[], char *arge[]) {
tpl_register("system.platform", &ctx.system.platform[DELIVERY_PLATFORM_RELEASE]);
tpl_register("deploy.jfrog.repo", &globals.jfrog.repo);
tpl_register("deploy.docker.registry", &ctx.deploy.docker.registry);
tpl_register("workaround.tox_posargs", &globals.workaround.tox_posargs);

// Set up PREFIX/etc directory information
// The user may manipulate the base directory path with OMC_SYSCONFDIR
Expand Down
58 changes: 57 additions & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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;
}

0 comments on commit a31a9b7

Please sign in to comment.