diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 7c760e50..f11d2d62 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -92,14 +92,10 @@ static Usage usage = { // clang-format on static void parseByte(uint16_t &output, char name) { - if (musl_optarg[0] == '\0') { - fatal("Argument to option '-%c' may not be empty", name); - } - if (std::optional value = parseWholeNumber(musl_optarg); !value) { - fatal("Expected number as argument to option '-%c', got \"%s\"", name, musl_optarg); - } else if (value > 0xFF) { - fatal("Argument to option '-%c' is larger than 255: %" PRIu64, name, *value); + fatal("Invalid argument for option '-%c'", name); + } else if (*value > 0xFF) { + fatal("Argument for option '-%c' must be between 0 and 0xFF", name); } else { output = *value; } @@ -117,9 +113,11 @@ static void initLogo() { if (strcmp(options.logoFilename, "-")) { logoFile = fopen(options.logoFilename, "rb"); } else { + // LCOV_EXCL_START options.logoFilename = ""; (void)setmode(STDIN_FILENO, O_BINARY); logoFile = stdin; + // LCOV_EXCL_STOP } if (!logoFile) { // LCOV_EXCL_START diff --git a/test/fix/bad-custom-logo.1bpp b/test/fix/bad-custom-logo.1bpp new file mode 100644 index 00000000..1b1cb4d4 Binary files /dev/null and b/test/fix/bad-custom-logo.1bpp differ diff --git a/test/fix/bad-custom-logo.err b/test/fix/bad-custom-logo.err new file mode 100644 index 00000000..05615c86 --- /dev/null +++ b/test/fix/bad-custom-logo.err @@ -0,0 +1 @@ +FATAL: "bad-custom-logo.1bpp" is not 48 bytes diff --git a/test/fix/bad-custom-logo.flags b/test/fix/bad-custom-logo.flags new file mode 100644 index 00000000..2cb3a012 --- /dev/null +++ b/test/fix/bad-custom-logo.flags @@ -0,0 +1 @@ +-L ./bad-custom-logo.1bpp diff --git a/test/fix/deprecated-overwrite.bin b/test/fix/deprecated-overwrite.bin new file mode 100644 index 00000000..512fb85a Binary files /dev/null and b/test/fix/deprecated-overwrite.bin differ diff --git a/test/fix/deprecated-overwrite.err b/test/fix/deprecated-overwrite.err new file mode 100644 index 00000000..5cdef849 --- /dev/null +++ b/test/fix/deprecated-overwrite.err @@ -0,0 +1 @@ +warning: '-O' is deprecated; use '-Wno-overwrite' instead [-Wobsolete] diff --git a/test/fix/deprecated-overwrite.flags b/test/fix/deprecated-overwrite.flags new file mode 100644 index 00000000..bc6ebd5e --- /dev/null +++ b/test/fix/deprecated-overwrite.flags @@ -0,0 +1 @@ +-O -v diff --git a/test/fix/deprecated-overwrite.gb b/test/fix/deprecated-overwrite.gb new file mode 100644 index 00000000..53188fd2 Binary files /dev/null and b/test/fix/deprecated-overwrite.gb differ diff --git a/test/fix/disable-warnings.bin b/test/fix/disable-warnings.bin new file mode 100644 index 00000000..ab24776d Binary files /dev/null and b/test/fix/disable-warnings.bin differ diff --git a/test/fix/disable-warnings.flags b/test/fix/disable-warnings.flags new file mode 100644 index 00000000..78739d7e --- /dev/null +++ b/test/fix/disable-warnings.flags @@ -0,0 +1,2 @@ +-w -m mbc3+ram -r 0 +The "-w" suppresses "-Wmbc" and "-Woverwrite" warnings diff --git a/test/fix/disable-warnings.gb b/test/fix/disable-warnings.gb new file mode 100644 index 00000000..ac86ca57 Binary files /dev/null and b/test/fix/disable-warnings.gb differ diff --git a/test/fix/invalid-color.err b/test/fix/invalid-color.err new file mode 100644 index 00000000..1a404429 --- /dev/null +++ b/test/fix/invalid-color.err @@ -0,0 +1 @@ +FATAL: Invalid argument for option '--color' diff --git a/test/fix/invalid-color.flags b/test/fix/invalid-color.flags new file mode 100644 index 00000000..a69ba475 --- /dev/null +++ b/test/fix/invalid-color.flags @@ -0,0 +1 @@ +--color=chartreuse diff --git a/test/fix/invalid-numeric-arg.err b/test/fix/invalid-numeric-arg.err new file mode 100644 index 00000000..afe82baa --- /dev/null +++ b/test/fix/invalid-numeric-arg.err @@ -0,0 +1 @@ +FATAL: Invalid argument for option '-r' diff --git a/test/fix/invalid-numeric-arg.flags b/test/fix/invalid-numeric-arg.flags new file mode 100644 index 00000000..a11267b1 --- /dev/null +++ b/test/fix/invalid-numeric-arg.flags @@ -0,0 +1 @@ +-r invalid diff --git a/test/fix/invalid-numeric-range.err b/test/fix/invalid-numeric-range.err new file mode 100644 index 00000000..5debeb24 --- /dev/null +++ b/test/fix/invalid-numeric-range.err @@ -0,0 +1 @@ +FATAL: Argument for option '-r' must be between 0 and 0xFF diff --git a/test/fix/invalid-numeric-range.flags b/test/fix/invalid-numeric-range.flags new file mode 100644 index 00000000..1e090731 --- /dev/null +++ b/test/fix/invalid-numeric-range.flags @@ -0,0 +1 @@ +-r 999 diff --git a/test/fix/multiple-to-one.err b/test/fix/multiple-to-one.err new file mode 100644 index 00000000..1831bee3 --- /dev/null +++ b/test/fix/multiple-to-one.err @@ -0,0 +1,17 @@ +FATAL: If '-o' is set then only a single input file may be specified +Usage: rgbfix [-hjOsVvw] [-C | -c] [-f ] [-i ] + [-k ] [-L ] [-l ] + [-m ] [-n ] [-p ] + [-r ] [-t ] [-W warning] ... + +Useful options: + -m, --mbc-type set the MBC type byte to this value; "-m help" + or "-m list" prints the accepted values + -p, --pad-value pad to the next valid size using this value + -r, --ram-size set the cart RAM size byte to this value + -o, --output set the output file + -V, --version print RGBFIX version and exit + -v, --validate fix the header logo and both checksums ("-f lhg") + -W, --warning enable or disable warnings + +For more help, use "man rgbfix" or go to https://rgbds.gbdev.io/docs/ diff --git a/test/fix/no-exist.err b/test/fix/no-exist.err new file mode 100644 index 00000000..2a70e7a5 --- /dev/null +++ b/test/fix/no-exist.err @@ -0,0 +1,2 @@ +error: Failed to open "no-exist" for reading+writing: No such file or directory +Fixing "no-exist" failed with 1 error diff --git a/test/fix/no-input.err b/test/fix/no-input.err new file mode 100644 index 00000000..6f31f6da --- /dev/null +++ b/test/fix/no-input.err @@ -0,0 +1,17 @@ +FATAL: No input file specified (pass "-" to read from standard input) +Usage: rgbfix [-hjOsVvw] [-C | -c] [-f ] [-i ] + [-k ] [-L ] [-l ] + [-m ] [-n ] [-p ] + [-r ] [-t ] [-W warning] ... + +Useful options: + -m, --mbc-type set the MBC type byte to this value; "-m help" + or "-m list" prints the accepted values + -p, --pad-value pad to the next valid size using this value + -r, --ram-size set the cart RAM size byte to this value + -o, --output set the output file + -V, --version print RGBFIX version and exit + -v, --validate fix the header logo and both checksums ("-f lhg") + -W, --warning enable or disable warnings + +For more help, use "man rgbfix" or go to https://rgbds.gbdev.io/docs/ diff --git a/test/fix/noexist.err b/test/fix/noexist.err deleted file mode 100644 index c4940146..00000000 --- a/test/fix/noexist.err +++ /dev/null @@ -1,2 +0,0 @@ -error: Failed to open "noexist" for reading+writing: No such file or directory -Fixing "noexist" failed with 1 error diff --git a/test/fix/ramful-mbc-2-kb.err b/test/fix/ramful-mbc-2-kb.err new file mode 100644 index 00000000..89d4b45f --- /dev/null +++ b/test/fix/ramful-mbc-2-kb.err @@ -0,0 +1 @@ +warning: RAM size 1 (2 KiB) was specified for MBC "MBC1+RAM" [-Wmbc] diff --git a/test/fix/ramful-mbc-2-kb.flags b/test/fix/ramful-mbc-2-kb.flags new file mode 100644 index 00000000..633469c0 --- /dev/null +++ b/test/fix/ramful-mbc-2-kb.flags @@ -0,0 +1 @@ +-m mbc1+ram -r 1 diff --git a/test/fix/ramful-mbc-2-kb.gb b/test/fix/ramful-mbc-2-kb.gb new file mode 100644 index 00000000..b049171e Binary files /dev/null and b/test/fix/ramful-mbc-2-kb.gb differ diff --git a/test/fix/rom-ram-not-2-kb.err b/test/fix/rom-ram-not-2-kb.err new file mode 100644 index 00000000..19029e4e --- /dev/null +++ b/test/fix/rom-ram-not-2-kb.err @@ -0,0 +1,2 @@ +warning: MBC "rom+ram" is under-specified and poorly supported [-Wmbc] +warning: MBC "ROM+RAM" should have 2 KiB of RAM ("-r 1") [-Wmbc] diff --git a/test/fix/rom-ram-not-2-kb.flags b/test/fix/rom-ram-not-2-kb.flags new file mode 100644 index 00000000..32834b1f --- /dev/null +++ b/test/fix/rom-ram-not-2-kb.flags @@ -0,0 +1 @@ +-m rom+ram -r 2 diff --git a/test/fix/rom-ram-not-2-kb.gb b/test/fix/rom-ram-not-2-kb.gb new file mode 100644 index 00000000..35368a30 Binary files /dev/null and b/test/fix/rom-ram-not-2-kb.gb differ diff --git a/test/fix/test.sh b/test/fix/test.sh index 46e2586d..550a5beb 100755 --- a/test/fix/test.sh +++ b/test/fix/test.sh @@ -12,6 +12,16 @@ cd "$tmpdir" || exit # shellcheck disable=SC2064 trap "cd; rm -rf ${tmpdir@Q}" EXIT +if which cygpath &>/dev/null; then + # MinGW needs the Windows path substituted but with forward slash separators; + # Cygwin has `cygpath` but just needs the original path substituted. + subst1="$(printf '%s\n' "$src" | sed 's:[][\/.^$*]:\\&:g')" + subst2="$(printf '%s\n' "$(cygpath -w "$src")" | sed -e 's:\\:/:g' -e 's:[][\/.^$*]:\\&:g')" + src_subst="$src/\\|$subst1/\\|$subst2/" +else + src_subst="$src/" +fi + tests=0 failed=0 rc=0 @@ -83,9 +93,9 @@ runTest () { else desired_errname=/dev/null fi - sed "s/$subst//g" out.out | tryDiff "$desired_outname" - "$1.out${variant}" + sed -e "s/$subst//g" -e "s#$src_subst##g" out.out | tryDiff "$desired_outname" - "$1.out${variant}" (( our_rc = our_rc || $? )) - sed "s/$subst//g" out.err | tryDiff "$desired_errname" - "$1.err${variant}" + sed -e "s/$subst//g" -e "s#$src_subst##g" out.err | tryDiff "$desired_errname" - "$1.err${variant}" (( our_rc = our_rc || $? )) if [[ -r "$2/$1.gb" ]]; then @@ -101,6 +111,16 @@ runTest () { done } +runSpecialTest () { + name="$1" + shift + echo "${bold}${green}${name}...${rescolors}${resbold}" + eval "$RGBFIX" "$@" '2>out.err' + rc=$((rc || $? != 1)) + tryDiff "$src/${name}.err" out.err "${name}.err" + rc=$((rc || $?)) +} + rm -f padding*_* # Delete padding test cases generated but not deleted (e.g. interrupted) progress=1 @@ -108,6 +128,15 @@ for i in "$src"/*.flags; do runTest "$(basename "$i" .flags)" "$src" done +# Check that RGBFIX errors out when inputting a non-existent file +runSpecialTest no-exist no-exist + +# Check that RGBFIX errors out when not inputting any file +runSpecialTest no-input + +# Check that RGBFIX errors out when inputting multiple files with an output file +runSpecialTest multiple-to-one one two three -o multiple-to-one + # Check the result with all different padding bytes echo "${bold}Checking padding...${resbold}" cp "$src"/padding{,-large,-larger}.bin . @@ -124,12 +153,6 @@ for (( i=0; i < 10; ++i )); do done echo "${bold}Done checking padding!${resbold}" -# Check that RGBFIX errors out when inputting a non-existent file... -$RGBFIX noexist 2>out.err -rc=$((rc || $? != 1)) -tryDiff "$src/noexist.err" out.err noexist.err -rc=$((rc || $?)) - if [[ "$failed" -eq 0 ]]; then echo "${bold}${green}All ${tests} tests passed!${rescolors}${resbold}" else