Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.testing: add methods tmpDirPath, getTestArgs, buildExe #11138

Merged
merged 3 commits into from
Mar 27, 2022

Conversation

matu3ba
Copy link
Contributor

@matu3ba matu3ba commented Mar 12, 2022

continuation of #11093 to simplify testing IPC

  • use cases
    • get path to temporary directory
    • get the test arguments inside test block for reusage
    • build executables from text within test blocks, ie to test IPC
  • missing conventions
    • how to name and debug test cases
    • where do simple+repititve build commands for testing belong

@matu3ba
Copy link
Contributor Author

matu3ba commented Mar 12, 2022

Question for next PR: Should there be a method to create a zig program from a string inside tests or where should it reside?
I woudl envision such a method to contain something along the following with an interface buildExeInTmp(allocator, progstr, binfile) ![]const u8 and the exe as return value.

    var it = try std.process.argsWithAllocator(allocator);
    const testargs = try testing.getTestArgs(&it);
    defer it.deinit(); // no-op unless WASI or Windows

    var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
    defer tmp.cleanup();
    const tmpdirpath = try testing.tmpDirPath(allocator, &tmp);
    defer allocator.free(tmpdirpath);
    const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin'
    const suffix_zig = ".zig";
    const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name });
    defer allocator.free(child_path);
    const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig });
    defer allocator.free(child_zig);

    try tmp.dir.writeFile("child.zig", childstr);
    try testing.buildExe(testargs.zigexec, child_zig, child_path);

That way, building (multiple) executables to a tmp folder would be exactly 1 properly checked function from within test blocks.

Alternatively, I will implement a shortcut to obtain child_zig and child_path (that means absolute path to zig file and absolute path to binary).

@matu3ba
Copy link
Contributor Author

matu3ba commented Mar 12, 2022

Some functionality is redundant to src/test.zig, but unfortunately this is no available for user programs, because it is not part of lib/std.
Any advice how to refactor src/test.zig or how to proceed would be nice.

lib/std/testing.zig Outdated Show resolved Hide resolved
Jan Philipp Hafer added 2 commits March 13, 2022 12:40
continuation of ziglang#11093 to simplify testing IPC

* use cases
  - get path to temporary directory
  - get the test arguments inside test block for reusage
  - build executables from text within test blocks, ie to test IPC
* missing conventions
  - how to name and debug test cases
  - where do simple+repititve build commands for testing belong
@iddev5
Copy link
Contributor

iddev5 commented Mar 19, 2022

Question for next PR:

Its a good suggestion but eventually similar functions would be needed for all other zig operations like run, buildLib etc.
What could instead be done is a function fn writeZigFileInTmp(allocator, progstr, binfile) ![]const u8 which would return the final path of file (which is child_zig) in your test, on which we can then manually do std.fs.path.dirname(child_zig).? to get child_path.

@Vexu Vexu merged commit dbbda0f into ziglang:master Mar 27, 2022
@matu3ba matu3ba deleted the process_pipe branch March 28, 2022 22:48
@matu3ba
Copy link
Contributor Author

matu3ba commented Mar 29, 2022

@iddev5 With your proposed solution I get

    const zigfile_path = try tmp.writeZigFile(std.testing.allocator, childstr, child_name);
    defer std.testing.allocator.free(zigfile_path);
    const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters
    try testing.buildExe(testargs.zigexec, zigfile_path, binary);

because we still need the path to the resulting binary for buildExe.
I think requiring to copy paste from users with an example for the subslice should be fine, since
we cant do it more efficiently than the subslice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants