-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement a rudimentory test framework and CI
- Loading branch information
Showing
12 changed files
with
189 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run tests | ||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
run-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
submodules: true | ||
- name: setup ripgrep and gcc | ||
run: | | ||
sudo apt-get update | ||
sudo apt install build-essential | ||
sudo apt install ripgrep | ||
- name: run tests | ||
run: | | ||
make run_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
gsx | ||
test/test | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#define _GNU_SOURCE // make popen available | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
|
||
#define CHUNK_SIZE 64*100 | ||
|
||
char* read_file(char* file_name) { | ||
FILE *f = fopen(file_name, "rb"); | ||
|
||
if (f == NULL) { | ||
fprintf(stderr, "Couldn't open %s", file_name); | ||
fprintf(stderr, "%d", errno); | ||
|
||
exit(EXIT_FAILURE); | ||
} | ||
if (fseek(f, 0, SEEK_END) != 0) { | ||
fprintf(stderr, "Error reading %s", file_name); | ||
exit(EXIT_FAILURE); | ||
} | ||
long size = ftell(f); | ||
char* content = (char *)malloc((size+1) * sizeof(char)); | ||
rewind(f); | ||
|
||
fread(content, 1, size, f); | ||
content[size] = '\0'; | ||
|
||
fclose(f); | ||
return content; | ||
} | ||
|
||
char* read_command(char* command) { | ||
char *result = (char *)malloc(CHUNK_SIZE * sizeof(char)); | ||
size_t bytes_length = 0; | ||
size_t length = 0; | ||
|
||
FILE *output = popen(command, "r"); | ||
if (output == NULL) { | ||
fprintf(stderr, "Error running command: %s\n", command); | ||
exit(EXIT_FAILURE); | ||
} | ||
while((bytes_length = fread(result, 1, CHUNK_SIZE, output)) > 0) { | ||
length += bytes_length; | ||
result = (char *)realloc(result, (length + 1) * sizeof(char)); | ||
} | ||
result[length] = '\0'; | ||
|
||
pclose(output); | ||
return result; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
char *read_file(const char* file_path); | ||
char *read_command(const char* command); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test/samples/file.tsx:8:<Button size="large"> | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test/samples/file.tsx:9:<Button variant="secondary"> | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test/samples/file.tsx:5:<Button variant="primary" size="large"> | ||
-- | ||
test/samples/file.tsx:9:<Button variant="secondary"> | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Test = () => { | ||
return ( | ||
<Box variant="primary" onClick={() => console.log("test")}> | ||
<div> | ||
<Button variant="primary" size="large"> | ||
Button | ||
</Button> | ||
<Button size="large">Button</Button> | ||
<Button variant="secondary">Button</Button> | ||
</div> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
test/samples/file.tsx:5:<Button variant="primary" size="large"> | ||
-- | ||
test/samples/file.tsx:8:<Button size="large"> | ||
-- | ||
test/samples/file.tsx:9:<Button variant="secondary"> | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
#include "../src/utils.h" | ||
|
||
typedef const char* Filepath; | ||
typedef const char* Command; | ||
typedef const char* Name; | ||
|
||
typedef struct { | ||
Name name; | ||
Command command; | ||
Filepath file; | ||
} Test; | ||
|
||
|
||
bool run_test(Test* test) { | ||
bool success = true; | ||
char *result = read_command(test->command); | ||
char *expected = read_file(test->file); | ||
|
||
if (strlen(result) > 0 && memcmp(result, expected, strlen(result)) == 0) { | ||
printf("%s: OK\n", test->name); | ||
} else { | ||
printf("%s: KO\n", test->name); | ||
printf("Expected: \n"); | ||
printf("%s\n", expected); | ||
printf("Got: \n"); | ||
printf("%s\n", result); | ||
success = false; | ||
} | ||
printf("----------------------------------------\n"); | ||
free(expected); | ||
free(result); | ||
return success; | ||
} | ||
|
||
Test tests[] = { | ||
(Test) { | ||
.name = "Button", | ||
.command = "./gsx test/samples/ Button", | ||
.file = "./test/simple.txt", | ||
}, | ||
(Test) { | ||
.name = "Button.^variant", | ||
.command = "./gsx test/samples/ Button.^variant", | ||
.file = "./test/exclude_one_prop.txt", | ||
}, | ||
(Test) { | ||
.name = "Button.variant", | ||
.command = "./gsx test/samples/ Button.variant", | ||
.file = "./test/one_prop.txt", | ||
}, | ||
(Test) { | ||
.name = "Button.variant,^size", | ||
.command = "./gsx test/samples/ Button.variant,^size", | ||
.file = "./test/include_one_exclude_one.txt", | ||
}, | ||
}; | ||
|
||
int main(void) { | ||
size_t nb_test = sizeof(tests) / sizeof(tests[0]); | ||
printf("Running all tests\n\n"); | ||
int success = 0; | ||
for (size_t i = 0; i < nb_test; ++i) { | ||
if (!run_test(&tests[i])) { | ||
success = 1; | ||
} | ||
} | ||
return success; | ||
} | ||
|