Просмотр исходного кода

Added testing ability

For the full details, see patches/README.md :-)
Dave Gauer 4 лет назад
Родитель
Сommit
03a09639c6
49 измененных файлов с 160 добавлено и 9 удалено
  1. 1 0
      .gitignore
  2. 9 4
      build.zig
  3. 4 0
      patches/01_hello.patch
  4. 4 0
      patches/02_std.patch
  5. 1 0
      patches/03_assignment.patch
  6. 1 0
      patches/04_arrays.patch
  7. 1 0
      patches/05_arrays2.patch
  8. 1 0
      patches/06_strings.patch
  9. 1 0
      patches/07_strings2.patch
  10. 1 0
      patches/08_quiz.patch
  11. 1 0
      patches/09_if.patch
  12. 1 0
      patches/10_if2.patch
  13. 1 0
      patches/11_while.patch
  14. 1 0
      patches/12_while2.patch
  15. 1 0
      patches/13_while3.patch
  16. 1 0
      patches/14_while4.patch
  17. 1 0
      patches/15_for.patch
  18. 1 0
      patches/16_for2.patch
  19. 1 0
      patches/17_quiz2.patch
  20. 1 0
      patches/18_functions.patch
  21. 1 0
      patches/19_functions2.patch
  22. 1 0
      patches/20_quiz3.patch
  23. 1 0
      patches/21_errors.patch
  24. 1 0
      patches/22_errors2.patch
  25. 1 0
      patches/23_errors3.patch
  26. 1 0
      patches/24_errors4.patch
  27. 1 0
      patches/25_errors5.patch
  28. 1 0
      patches/26_hello2.patch
  29. 1 0
      patches/27_defer.patch
  30. 1 0
      patches/28_defer2.patch
  31. 1 0
      patches/29_errdefer.patch
  32. 1 0
      patches/30_switch.patch
  33. 1 0
      patches/31_switch2.patch
  34. 1 0
      patches/32_unreachable.patch
  35. 1 0
      patches/33_iferror.patch
  36. 1 0
      patches/34_quiz4.patch
  37. 1 0
      patches/35_enums.patch
  38. 1 0
      patches/36_enums2.patch
  39. 1 0
      patches/37_structs.patch
  40. 1 0
      patches/38_structs2.patch
  41. 1 0
      patches/39_pointers.patch
  42. 1 0
      patches/40_pointers2.patch
  43. 1 0
      patches/41_pointers3.patch
  44. 1 0
      patches/42_pointers4.patch
  45. 1 0
      patches/43_pointers5.patch
  46. 1 0
      patches/44_quiz5.patch
  47. 31 5
      patches/README.md
  48. 45 0
      patches/eowyn.sh
  49. 24 0
      patches/gollum.sh

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 *.swp
 zig-cache/
 answers/
+patches/healed/

+ 9 - 4
build.zig

@@ -330,15 +330,17 @@ pub fn build(b: *Builder) void {
 
     var prev_chain_verify = verify_all;
 
+    const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
+
     for (exercises) |ex| {
         const base_name = ex.baseName();
         const file_path = std.fs.path.join(b.allocator, &[_][]const u8{
-            "exercises", ex.main_file,
+            if (use_healed) "patches/healed" else "exercises", ex.main_file,
         }) catch unreachable;
         const build_step = b.addExecutable(base_name, file_path);
         build_step.install();
 
-        const verify_step = ZiglingStep.create(b, ex);
+        const verify_step = ZiglingStep.create(b, ex, use_healed);
 
         const key = ex.key();
 
@@ -375,13 +377,15 @@ const ZiglingStep = struct {
     step: Step,
     exercise: Exercise,
     builder: *Builder,
+    use_healed: bool,
 
-    pub fn create(builder: *Builder, exercise: Exercise) *@This() {
+    pub fn create(builder: *Builder, exercise: Exercise, use_healed: bool) *@This() {
         const self = builder.allocator.create(@This()) catch unreachable;
         self.* = .{
             .step = Step.init(.Custom, exercise.main_file, builder.allocator, make),
             .exercise = exercise,
             .builder = builder,
+            .use_healed = use_healed,
         };
         return self;
     }
@@ -490,7 +494,8 @@ const ZiglingStep = struct {
             zig_args.append(@tagName(builder.color)) catch unreachable;
         }
 
-        const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ "exercises", self.exercise.main_file }) catch unreachable;
+        const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ 
+            if (self.use_healed) "patches/healed" else "exercises", self.exercise.main_file }) catch unreachable;
         zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable;
 
         zig_args.append("--cache-dir") catch unreachable;

+ 4 - 0
patches/01_hello.patch

@@ -0,0 +1,4 @@
+19c19
+< fn main() void {
+---
+> pub fn main() void {

+ 4 - 0
patches/02_std.patch

@@ -0,0 +1,4 @@
+14c14
+< ??? = @import("std");
+---
+> const std = @import("std");

+ 1 - 0
patches/03_assignment.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/04_arrays.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/05_arrays2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/06_strings.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/07_strings2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/08_quiz.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/09_if.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/10_if2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/11_while.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/12_while2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/13_while3.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/14_while4.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/15_for.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/16_for2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/17_quiz2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/18_functions.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/19_functions2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/20_quiz3.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/21_errors.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/22_errors2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/23_errors3.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/24_errors4.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/25_errors5.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/26_hello2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/27_defer.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/28_defer2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/29_errdefer.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/30_switch.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/31_switch2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/32_unreachable.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/33_iferror.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/34_quiz4.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/35_enums.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/36_enums2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/37_structs.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/38_structs2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/39_pointers.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/40_pointers2.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/41_pointers3.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/42_pointers4.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/43_pointers5.patch

@@ -0,0 +1 @@
+

+ 1 - 0
patches/44_quiz5.patch

@@ -0,0 +1 @@
+

+ 31 - 5
patches/README.md

@@ -1,9 +1,35 @@
-# The ziglings/patches Directory
+# No Peeking! :-)
 
-This is how ziglings is tested.
+Welcome to the ziglings/patches directory. This is how ziglings is tested.
 
-The patches fix the broken exercises so that they work again.
+The patches fix the broken exercises so that they work again, which means the
+answers are here, so no peeking!
 
-No peeking! :-)
+## Éowyn
+
+A Bash shell script named `eowyn.sh` dwells here. She heals the little broken
+programs and places them in a `healed` directory, which is not committed to the
+repo.
+
+```bash
+$ ./eowyn.sh
+```
+
+(If you invoke her from elsewhere, she'll come here to ply her trade.)
+
+The `build.zig` build script at the heart of Ziglings has a top-secret option
+which tells it to test from the `patches/healed/` dir rather than `exercises/`:
+
+```bash
+$ zig build -Dhealed [step]
+```
+
+Éowyn tests all healed programs using this secret option.
+
+
+## Gollum
+
+Another Bash shell script named `gollum.sh` may also be found. He snatches the
+original answers and stows them in his secret answers stash. If you leave him
+alone, he'll leave you alone.
 
-(Further tooling and explanation goes here.)

+ 45 - 0
patches/eowyn.sh

@@ -0,0 +1,45 @@
+#!/bin/bash
+#
+#     "I will be a shieldmaiden no longer,
+#      nor vie with the great Riders, nor
+#      take joy only in the songs of slaying.
+#      I will be a healer, and love all things
+#      that grow and are not barren."
+#             Éowyn, The Return of the King
+#
+#
+# This script shall heal the little broken programs
+# using the patches in this directory and convey them
+# to convalesce in the healed directory.
+#
+
+# We run from the patches dir. Go there now if not already.
+cd $(dirname $(which $0))
+pwd # Show it upon the screen so all shall be made apparent.
+
+# Create healed/ directory here if it doesn't already exist.
+mkdir -p healed
+
+# Cycle through all the little broken Zig applications.
+for broken in ../exercises/*.zig
+do
+    # Remove the dir and extension, rendering the True Name.
+    true_name=$(basename $broken .zig)
+
+    if [[ -f $true_name.patch ]]
+    then
+        # Apply the bandages to the wounds, grow new limbs, let
+        # new life spring into the broken bodies of the fallen.
+        echo Healing $true_name...
+        patch --output=healed/$true_name.zig $broken $true_name.patch
+    else
+        echo Cannot heal $true_name. Making empty patch.
+        echo > $true_name.patch
+    fi
+done
+
+# Return to the home of our ancestors.
+cd ..
+
+# Test the healed exercises. May the compiler have mercy upon us.
+zig build -Dhealed

+ 24 - 0
patches/gollum.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+#     "It isn't fair, my precious, is it,
+#      to ask us what it's got in it's
+#      nassty little pocketsess?"
+#             Gollum, The Hobbit, or There and Back Again 
+#
+
+cd $(dirname $(which $0))
+f=$(basename ../exercises/$1*.zig .zig 2> /dev/null)
+b=../exercises/$f.zig
+a=../answers/$f.zig
+p=$f.patch
+
+printf "\tf: '$f'\n\tb: '$b'\n\ta: '$a'\n"
+
+if [[ ! -f $b ]]; then echo "We hates it!"; exit 1; fi
+if [[ ! -a $a ]]; then echo "Where is it? Where is the answer, precious?"; exit; fi
+
+echo Hisssss!
+
+diff $b $a > $p
+
+cat $p