Files
dusk/test/save/savetestfixture.c
T
YourWishesandClaude Sonnet 5 cebd3d81e7 Wrap long lines to fit within 80 columns across the codebase
Pure whitespace/line-break reformatting (braces, newlines, and line
continuations matching this codebase's existing wrap conventions) - no
logic, string content, or identifiers changed anywhere. Confirmed via
diff against the pre-change tree and by rebuilding + re-running the
affected test suites, which produce identical pass/fail results.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-05 13:13:10 -05:00

86 lines
2.0 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
// nftw()/FTW_DEPTH/FTW_PHYS are glibc extensions gated behind this.
#define _XOPEN_SOURCE 700
#include "savetestfixture.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "util/string.h"
#include <ftw.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char_t SAVE_TEST_FIXTURE_HOME[PATH_MAX];
static char_t SAVE_TEST_FIXTURE_REAL_HOME[PATH_MAX];
static bool_t SAVE_TEST_FIXTURE_HAD_REAL_HOME;
static int_t saveTestFixtureRemoveEntry(
const char *path,
const struct stat *statBuffer,
int typeFlag,
struct FTW *ftwBuffer
) {
return remove(path);
}
int saveTestFixtureSetup(void **state) {
const char_t *realHome = getenv("HOME");
SAVE_TEST_FIXTURE_HAD_REAL_HOME = realHome != NULL;
if(realHome != NULL) {
stringCopy(
SAVE_TEST_FIXTURE_REAL_HOME, realHome,
sizeof(SAVE_TEST_FIXTURE_REAL_HOME) - 1
);
}
stringCopy(
SAVE_TEST_FIXTURE_HOME, "/tmp/dusk-save-test-XXXXXX",
sizeof(SAVE_TEST_FIXTURE_HOME) - 1
);
assertNotNull(mkdtemp(SAVE_TEST_FIXTURE_HOME), "Failed to create temp $HOME");
assertTrue(setenv("HOME", SAVE_TEST_FIXTURE_HOME, 1) == 0, "setenv failed");
errorret_t ret = saveInit();
assertTrue(errorIsOk(ret), "saveInit failed in test fixture");
return 0;
}
int saveTestFixtureTeardown(void **state) {
nftw(
SAVE_TEST_FIXTURE_HOME, saveTestFixtureRemoveEntry, 16,
FTW_DEPTH | FTW_PHYS
);
if(SAVE_TEST_FIXTURE_HAD_REAL_HOME) {
setenv("HOME", SAVE_TEST_FIXTURE_REAL_HOME, 1);
} else {
unsetenv("HOME");
}
memoryZero(&SAVE, sizeof(SAVE));
return 0;
}
void saveTestFixtureBlockSaveDirectory(void) {
const char_t *home = getenv("HOME");
assertNotNull(home, "Fixture $HOME must be set before blocking it");
char_t path[PATH_MAX];
stringFormat(path, sizeof(path), "%s/.dusk", home);
FILE *file = fopen(path, "w");
assertNotNull(file, "Failed to create blocking file");
fclose(file);
}