mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
test: Wrote a test for the list command.
This commit is contained in:
3
.envrc
3
.envrc
@@ -1,3 +1,4 @@
|
|||||||
use flake
|
use flake
|
||||||
|
|
||||||
export PATH=".:/home/spencer/github.com/envr-zig/deps/zig:/home/spencer/github.com/envr-zig/deps/zls:$PATH"
|
ROOT="/home/spencer/Desktop/envr"
|
||||||
|
export PATH=".:${ROOT}/deps/zig:${ROOT}/deps/zls:$PATH"
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
# dev env
|
# dev env
|
||||||
.direnv
|
.direnv
|
||||||
|
/.env
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
deps
|
deps
|
||||||
|
|||||||
18
src/Db.zig
18
src/Db.zig
@@ -46,11 +46,27 @@ pub fn open(
|
|||||||
defer private_keys.deinit(gpa);
|
defer private_keys.deinit(gpa);
|
||||||
|
|
||||||
for (opts.config.keys) |key| {
|
for (opts.config.keys) |key| {
|
||||||
|
// FIXME: cheating here
|
||||||
|
if (std.mem.startsWith(u8, key.private, "~/")) {
|
||||||
|
const key_path = try std.fs.path.join(gpa, &.{
|
||||||
|
opts.home,
|
||||||
|
key.private[2..],
|
||||||
|
});
|
||||||
|
private_keys.appendAssumeCapacity(key_path);
|
||||||
|
// defer gpa.free(key_path);
|
||||||
|
} else {
|
||||||
private_keys.appendAssumeCapacity(key.private);
|
private_keys.appendAssumeCapacity(key.private);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Pass key(s) from Config
|
// TODO: Pass key(s) from Config
|
||||||
try age.decrypt(io, gpa, private_keys.items, db_path, tmp_db_path);
|
try age.decrypt(io, gpa, private_keys.items, db_path, tmp_db_path);
|
||||||
|
|
||||||
|
for (opts.config.keys, 0..) |key, i| {
|
||||||
|
if (std.mem.startsWith(u8, key.private, "~/")) {
|
||||||
|
gpa.free(private_keys.items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +189,7 @@ const EnvFile = struct {
|
|||||||
sha256: []const u8,
|
sha256: []const u8,
|
||||||
contents: []const u8,
|
contents: []const u8,
|
||||||
|
|
||||||
fn deinit(self: *EnvFile, alloc: std.mem.Allocator) void {
|
pub fn deinit(self: *EnvFile, alloc: std.mem.Allocator) void {
|
||||||
alloc.free(self.path);
|
alloc.free(self.path);
|
||||||
alloc.free(self.remotes);
|
alloc.free(self.remotes);
|
||||||
alloc.free(self.sha256);
|
alloc.free(self.sha256);
|
||||||
|
|||||||
96
src/root.zig
96
src/root.zig
@@ -2,11 +2,11 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Io = std.Io;
|
const Io = std.Io;
|
||||||
|
|
||||||
const comma = @import("comma");
|
const Command = @import("comma").Command;
|
||||||
const Command = comma.Command;
|
|
||||||
|
|
||||||
const Config = @import("Config.zig");
|
const Config = @import("Config.zig");
|
||||||
const Db = @import("Db.zig");
|
const Db = @import("Db.zig");
|
||||||
|
const tabula = @import("./tabula.zig");
|
||||||
|
|
||||||
pub const root: Command = .new(.{
|
pub const root: Command = .new(.{
|
||||||
.name = "envr",
|
.name = "envr",
|
||||||
@@ -80,15 +80,18 @@ pub fn list(
|
|||||||
.tmp = tmp,
|
.tmp = tmp,
|
||||||
});
|
});
|
||||||
|
|
||||||
_ = try out.write("Path\n");
|
|
||||||
const files = try db.list(arena);
|
const files = try db.list(arena);
|
||||||
for (files) |file| {
|
defer arena.free(files);
|
||||||
// TODO: Table printer
|
|
||||||
try out.print("{s}\n", .{file.path});
|
try tabula.structs(@TypeOf(files[0]), files, out);
|
||||||
}
|
|
||||||
try out.flush();
|
try out.flush();
|
||||||
|
|
||||||
return db.close(io, arena); // TODO: Defer this
|
try db.close(io, arena); // TODO: Defer this
|
||||||
|
|
||||||
|
// TODO: Is this bad?
|
||||||
|
for (files) |file| {
|
||||||
|
@constCast(&file).deinit(arena);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
@@ -129,3 +132,80 @@ test "parse unknown" {
|
|||||||
|
|
||||||
try std.testing.expectEqual(.unknown, cmd);
|
try std.testing.expectEqual(.unknown, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "list returns a table" {
|
||||||
|
const io = std.testing.io;
|
||||||
|
const gpa = std.testing.allocator;
|
||||||
|
|
||||||
|
var tmp_dir = std.testing.tmpDir(.{});
|
||||||
|
defer tmp_dir.cleanup();
|
||||||
|
|
||||||
|
try tmp_dir.dir.createDir(io, "home", .default_dir);
|
||||||
|
try tmp_dir.dir.createDir(io, "home/.envr", .default_dir);
|
||||||
|
try tmp_dir.dir.createDir(io, "home/.ssh", .default_dir);
|
||||||
|
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
|
||||||
|
|
||||||
|
const tmp_dir_path = try tmp_dir.dir.realPathFileAlloc(io, ".", gpa);
|
||||||
|
defer gpa.free(tmp_dir_path);
|
||||||
|
|
||||||
|
const home = try std.fs.path.join(gpa, &.{ tmp_dir_path, "home" });
|
||||||
|
defer gpa.free(home);
|
||||||
|
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
|
||||||
|
defer gpa.free(tmp);
|
||||||
|
|
||||||
|
try std.Io.Dir.cwd().copyFile(
|
||||||
|
"fixtures/encrypted-single-file.db.age",
|
||||||
|
tmp_dir.dir,
|
||||||
|
"home/.envr/data.age",
|
||||||
|
io,
|
||||||
|
.{},
|
||||||
|
);
|
||||||
|
|
||||||
|
try std.Io.Dir.cwd().copyFile(
|
||||||
|
"fixtures/default_config.json",
|
||||||
|
tmp_dir.dir,
|
||||||
|
"home/.envr/config.json",
|
||||||
|
io,
|
||||||
|
.{},
|
||||||
|
);
|
||||||
|
|
||||||
|
try std.Io.Dir.cwd().copyFile(
|
||||||
|
"fixtures/insecure-test-key",
|
||||||
|
tmp_dir.dir,
|
||||||
|
"home/.ssh/id_ed25519",
|
||||||
|
io,
|
||||||
|
.{},
|
||||||
|
);
|
||||||
|
|
||||||
|
try std.Io.Dir.cwd().copyFile(
|
||||||
|
"fixtures/insecure-test-key.pub",
|
||||||
|
tmp_dir.dir,
|
||||||
|
"home/.ssh/id_ed25519.pub",
|
||||||
|
io,
|
||||||
|
.{},
|
||||||
|
);
|
||||||
|
|
||||||
|
var out: std.Io.Writer.Allocating = .init(gpa);
|
||||||
|
defer out.deinit();
|
||||||
|
|
||||||
|
// Run Test
|
||||||
|
|
||||||
|
try list(
|
||||||
|
io,
|
||||||
|
std.testing.allocator,
|
||||||
|
&out.writer,
|
||||||
|
home,
|
||||||
|
tmp,
|
||||||
|
);
|
||||||
|
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
\\Path
|
||||||
|
\\~/project/.env.example
|
||||||
|
\\
|
||||||
|
// \\┌─────────────────────────────┬──────┐
|
||||||
|
// \\│ DIRECTORY │ PATH │
|
||||||
|
// \\├─────────────────────────────┼──────┤
|
||||||
|
// \\│ /home/spencer/Desktop/envr/ │ .env │
|
||||||
|
// \\└─────────────────────────────┴──────┘
|
||||||
|
, try out.toOwnedSlice());
|
||||||
|
}
|
||||||
|
|||||||
26
src/tabula.zig
Normal file
26
src/tabula.zig
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const sep = "|";
|
||||||
|
|
||||||
|
pub fn structs(comptime T: type, items: []T, out: *std.Io.Writer) !void {
|
||||||
|
// Print Header
|
||||||
|
{
|
||||||
|
_ = try out.write(sep);
|
||||||
|
|
||||||
|
const fields = @typeInfo(T).@"struct".fields;
|
||||||
|
inline for (fields) |field| {
|
||||||
|
try out.print(" {s} {s}", .{ field.name, sep });
|
||||||
|
}
|
||||||
|
|
||||||
|
try out.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print body
|
||||||
|
for (items) |item| {
|
||||||
|
try out.print("{s}\n", .{item.path});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: fn determine_line_lengths(T: type, items: []T)
|
||||||
|
|
||||||
|
// TODO: test "returns a table" {}
|
||||||
Reference in New Issue
Block a user