Parse JSON if file is available.

pull/9/head
Joshua Potter 2023-11-23 15:01:58 -07:00
parent 1b8b454ccd
commit 9074792cfc
6 changed files with 67 additions and 3 deletions

View File

@ -10,7 +10,7 @@ configured like so:
```spec.json
{
versions: [...],
versions: [...]
}
```

View File

@ -42,7 +42,23 @@ read_spec_json(const struct Config *const config, cJSON **parsed) {
return 0;
}
// TODO: Need to parse the spec.json file.
// For simplicity, load the entire file into memory.
fseek(handle, 0, SEEK_END);
size_t file_size = ftell(handle);
fseek(handle, 0, SEEK_SET);
char *fcontent = malloc(file_size);
size_t read_count = fread(fcontent, 1, file_size, handle);
assert(read_count == file_size);
*parsed = cJSON_Parse(fcontent);
free(fcontent);
fclose(handle);
assert(false);
// Can use `cJSON_GetErrorPtr()` to get the actual error message.
if (!*parsed) {
return SJE_JSON_INVALID;
}
return 0;
}

View File

@ -22,6 +22,8 @@ int main(int argc, char *argv[]) {
sput_enter_suite("loader");
sput_run_test(test_read_spec_json_missing);
sput_run_test(test_read_spec_json_minimal);
sput_run_test(test_read_spec_json_invalid);
sput_finish_testing();

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "hello world"

View File

@ -0,0 +1 @@
{

View File

@ -34,4 +34,46 @@ static void test_read_spec_json_missing() {
free(root_dir);
}
static void test_read_spec_json_minimal() {
char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"};
char *root_dir = join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Config config = {
cwd,
root_dir,
"minimal_spec_json",
};
cJSON *parsed = 0;
enum SpecJsonError retval = read_spec_json(&config, &parsed);
sput_fail_unless(retval == 0, "minimal spec.json, success");
sput_fail_unless(parsed != 0, "minimal spec.json, parsed");
free(cwd);
free(root_dir);
}
static void test_read_spec_json_invalid() {
char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"};
char *root_dir = join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Config config = {
cwd,
root_dir,
"invalid_spec_json",
};
cJSON *parsed = 0;
enum SpecJsonError retval = read_spec_json(&config, &parsed);
sput_fail_unless(retval == SJE_JSON_INVALID, "invalid spec.json, JSON_INVALID");
sput_fail_unless(parsed == 0, "invalid spec.json, not parsed");
free(cwd);
free(root_dir);
}
#endif /* _BOOTSTRAP_TEST_LOADER */