From 9074792cfc6e318ef0d32e777458ecb1c72f2589 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Thu, 23 Nov 2023 15:01:58 -0700 Subject: [PATCH] Parse JSON if file is available. --- README.md | 2 +- src/loader.c | 20 ++++++++++-- test/runner.c | 2 ++ test/specs/invalid_spec_json/run.sh | 3 ++ test/specs/invalid_spec_json/spec.json | 1 + test/test_loader.h | 42 ++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/specs/invalid_spec_json/run.sh create mode 100644 test/specs/invalid_spec_json/spec.json diff --git a/README.md b/README.md index 4f8bc63..4b1a13d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ configured like so: ```spec.json { - versions: [...], + versions: [...] } ``` diff --git a/src/loader.c b/src/loader.c index fc2a360..883267c 100644 --- a/src/loader.c +++ b/src/loader.c @@ -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; } diff --git a/test/runner.c b/test/runner.c index e512b5e..1c95d6f 100644 --- a/test/runner.c +++ b/test/runner.c @@ -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(); diff --git a/test/specs/invalid_spec_json/run.sh b/test/specs/invalid_spec_json/run.sh new file mode 100644 index 0000000..c914ca6 --- /dev/null +++ b/test/specs/invalid_spec_json/run.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo "hello world" diff --git a/test/specs/invalid_spec_json/spec.json b/test/specs/invalid_spec_json/spec.json new file mode 100644 index 0000000..98232c6 --- /dev/null +++ b/test/specs/invalid_spec_json/spec.json @@ -0,0 +1 @@ +{ diff --git a/test/test_loader.h b/test/test_loader.h index 9d94100..b9b1283 100644 --- a/test/test_loader.h +++ b/test/test_loader.h @@ -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 */