Skip to content

Commit

Permalink
Unit test for checking the version number matches
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Feb 23, 2023
1 parent b7ebec6 commit 6a11224
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
- name: make
run: make -j $(nproc) WERROR=1

- name: Run build info checks
run: ./tests/run_build_info_checks.sh

- name: Run logic checks
run: ./tests/run_logic_checks.sh

Expand Down
4 changes: 2 additions & 2 deletions include/common/RabbitizerVersion.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* SPDX-FileCopyrightText: © 2022 Decompollaborate */
/* SPDX-FileCopyrightText: © 2022-2023 Decompollaborate */
/* SPDX-License-Identifier: MIT */

#ifndef RABBITIZER_VERSION_H
Expand All @@ -14,7 +14,7 @@ extern "C" {
// Header version
#define RAB_VERSION_MAJOR 1
#define RAB_VERSION_MINOR 5
#define RAB_VERSION_PATCH 10
#define RAB_VERSION_PATCH 9

#define RAB_VERSION_STR RAB_STRINGIFY(RAB_VERSION_MAJOR) "." RAB_STRINGIFY(RAB_VERSION_MINOR) "." RAB_STRINGIFY(RAB_VERSION_PATCH)

Expand Down
3 changes: 2 additions & 1 deletion include/common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ extern "C" {

#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof((arr)[0]))

#define RAB_STRINGIFY(x) #x
#define RAB_STRINGIFY2(x) #x
#define RAB_STRINGIFY(x) RAB_STRINGIFY2(x)

#define MASK(v, w) ((v) & ((1 << (w)) - 1))

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# SPDX-FileCopyrightText: © 2022 Decompollaborate
# SPDX-FileCopyrightText: © 2022-2023 Decompollaborate
# SPDX-License-Identifier: MIT

[project]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "1.5.10"
version = "1.5.9"
description = "MIPS instruction decoder"
# license = "MIT"
readme = "README.md"
Expand Down
105 changes: 105 additions & 0 deletions tests/c/build_info_checks/version_number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* SPDX-FileCopyrightText: © 2023 Decompollaborate */
/* SPDX-License-Identifier: MIT */

/**
* Checks every the version numbers of every built package to be the same
*/

#include "rabbitizer.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>


const char *const tomlPaths[] = {
"pyproject.toml",
"Cargo.toml",
};


long getFileSize(FILE *file) {
long current = ftell(file);
long totalSize;

fseek(file, 0L, SEEK_END);
totalSize = ftell(file);

fseek(file, current, SEEK_SET);

return totalSize;
}

#define VERSION_STR_TOML "version = \""

int doVersionCheck(const char *filepath, char *buffer) {
char *versionStrPtr = strstr(buffer, VERSION_STR_TOML);
char *endVersionStrPtr;

if (versionStrPtr == NULL) {
fprintf(stderr, "Could not find version string in file '%s'\n", filepath);
return 1;
}

// skip initial version stuff
versionStrPtr += strlen(VERSION_STR_TOML);

endVersionStrPtr = strstr(versionStrPtr, "\"");
if (endVersionStrPtr == NULL) {
fprintf(stderr, "Badly formatted version string in file '%s'\n", filepath);
return 1;
}

*endVersionStrPtr = '\0';

printf("file: '%s', version: '%s'\n", filepath, versionStrPtr);

if (strcmp(RabVersion_Str, versionStrPtr) != 0) {
fprintf(stderr, "Version of file '%s' does not match\n", filepath);
return 1;
}

return 0;
}

int main() {
size_t i;
int errorCount = 0;

printf("C package version: '%s'\n", RabVersion_Str);

for (i = 0; i < ARRAY_COUNT(tomlPaths); i++) {
const char *path = tomlPaths[i];
FILE *file;
long fileSize;
char *buffer;

assert(path != NULL);

file = fopen(path, "r");
if (file == NULL) {
fprintf(stderr, "Not able to open '%s'\n", path);
errorCount++;
continue;
}

fileSize = getFileSize(file);
buffer = malloc(fileSize * sizeof(char));
if (buffer == NULL) {
fclose(file);
fprintf(stderr, "Failed to malloc '%zu' bytes\n", fileSize * sizeof(char));
errorCount++;
continue;
}

fread(buffer, sizeof(char), fileSize, file);

errorCount += doVersionCheck(path, buffer);

free(buffer);
fclose(file);
}

return errorCount;
}
8 changes: 8 additions & 0 deletions tests/run_build_info_checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# SPDX-FileCopyrightText: © 2023 Decompollaborate
# SPDX-License-Identifier: MIT

set -e

./build/tests/c/build_info_checks/version_number.elf

0 comments on commit 6a11224

Please sign in to comment.