From 62bf63d36f153ecfb3276f0287ae58be1cb2c6d3 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 30 Mar 2021 11:37:59 +0100 Subject: [PATCH] librclone: add tests for build and execute them in the actions #4891 --- .github/workflows/build.yml | 8 +++++ librclone/ctest/Makefile | 3 ++ librclone/ctest/ctest.c | 65 ++++++++++++++++++++++++++++++++++--- librclone/librclone.go | 6 ++-- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 16f29fe1b..87990db6d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,6 +36,7 @@ jobs: check: true quicktest: true racequicktest: true + librclonetest: true deploy: true - job_name: mac_amd64 @@ -193,6 +194,13 @@ jobs: make racequicktest if: matrix.racequicktest + - name: Run librclone tests + shell: bash + run: | + make -C librclone/ctest test + make -C librclone/ctest clean + if: matrix.librclonetest + - name: Code quality test shell: bash run: | diff --git a/librclone/ctest/Makefile b/librclone/ctest/Makefile index ae57c1739..2de219101 100644 --- a/librclone/ctest/Makefile +++ b/librclone/ctest/Makefile @@ -10,5 +10,8 @@ ctest.o: ctest.c librclone.h librclone.a librclone.h: go build --buildmode=c-archive -o librclone.a github.com/rclone/rclone/librclone +test: ctest + ./ctest + clean: rm -f tmp ctest *.o *.a *.h *.gch diff --git a/librclone/ctest/ctest.c b/librclone/ctest/ctest.c index 69c92c48e..a3ee72b26 100644 --- a/librclone/ctest/ctest.c +++ b/librclone/ctest/ctest.c @@ -1,3 +1,6 @@ +/* +This is a very simple test/demo program for librclone's C interface +*/ #include #include #include @@ -14,21 +17,73 @@ void testRPC(char *method, char *in) { // noop command void testNoOp() { printf("test rc/noop\n"); - testRPC("rc/noop", - "{" + struct RcloneRPCResult out = RcloneRPC("rc/noop", "{" " \"p1\": [1,\"2\",null,4]," " \"p2\": { \"a\":1, \"b\":2 } " "}"); + printf("status: %d\n", out.Status); + printf("output: %s\n", out.Output); + const char *expected = + "{\n" + "\t\"p1\": [\n" + "\t\t1,\n" + "\t\t\"2\",\n" + "\t\tnull,\n" + "\t\t4\n" + "\t],\n" + "\t\"p2\": {\n" + "\t\t\"a\": 1,\n" + "\t\t\"b\": 2\n" + "\t}\n" + "}\n"; + if (strcmp(expected, out.Output) != 0) { + fprintf(stderr, "Wrong output.\nWant:\n%s\nGot:\n%s\n", expected, out.Output); + exit(EXIT_FAILURE); + } + if (out.Status != 200) { + fprintf(stderr, "Wrong status: want: %d: got: %d\n", 200, out.Status); + exit(EXIT_FAILURE); + } + free(out.Output); } // error command void testError() { printf("test rc/error\n"); - testRPC("rc/error", + struct RcloneRPCResult out = RcloneRPC("rc/error", "{" " \"p1\": [1,\"2\",null,4]," " \"p2\": { \"a\":1, \"b\":2 } " "}"); + printf("status: %d\n", out.Status); + printf("output: %s\n", out.Output); + const char *expected = + "{\n" + "\t\"error\": \"arbitrary error on input map[p1:[1 2 \\u003cnil\\u003e 4] p2:map[a:1 b:2]]\",\n" + "\t\"input\": {\n" + "\t\t\"p1\": [\n" + "\t\t\t1,\n" + "\t\t\t\"2\",\n" + "\t\t\tnull,\n" + "\t\t\t4\n" + "\t\t],\n" + "\t\t\"p2\": {\n" + "\t\t\t\"a\": 1,\n" + "\t\t\t\"b\": 2\n" + "\t\t}\n" + "\t},\n" + "\t\"path\": \"rc/error\",\n" + "\t\"status\": 500\n" + "}\n"; + if (strcmp(expected, out.Output) != 0) { + fprintf(stderr, "Wrong output.\nWant:\n%s\nGot:\n%s\n", expected, out.Output); + exit(EXIT_FAILURE); + } + if (out.Status != 500) { + fprintf(stderr, "Wrong status: want: %d: got: %d\n", 500, out.Status); + exit(EXIT_FAILURE); + } + free(out.Output); } // copy file using "operations/copyfile" command @@ -55,8 +110,8 @@ int main(int argc, char** argv) { testNoOp(); testError(); - testCopyFile(); - testListRemotes(); + /* testCopyFile(); */ + /* testListRemotes(); */ RcloneFinalize(); return EXIT_SUCCESS; diff --git a/librclone/librclone.go b/librclone/librclone.go index 1b6b0d4c3..40e693bee 100644 --- a/librclone/librclone.go +++ b/librclone/librclone.go @@ -79,7 +79,7 @@ func RcloneFinalize() { // // Output will be returned as a serialized JSON object // Status is a HTTP status return (200=OK anything else fail) -type RcloneRPCResult struct { +type RcloneRPCResult struct { //nolint:deadcode Output *C.char Status C.int } @@ -113,12 +113,12 @@ type RcloneMobileRPCResult struct { Status int } -// RcloneMobileRPCRPC this works the same as RcloneRPC but has an interface +// RcloneMobileRPC works the same as RcloneRPC but has an interface // optimised for gomobile, in particular the function signature is // valid under gobind rules. // // https://pkg.go.dev/golang.org/x/mobile/cmd/gobind#hdr-Type_restrictions -func RcloneMobileRPCRPC(method string, input string) (result RcloneMobileRPCResult) { +func RcloneMobileRPC(method string, input string) (result RcloneMobileRPCResult) { //nolint:deadcode output, status := callFunctionJSON(method, input) result.Output = output result.Status = status