|
| 1 | +local io = require("io") |
| 2 | +local os = require("os") |
| 3 | + |
| 4 | +local kcl_lib = require("kcl_lib") |
| 5 | + |
| 6 | +describe("kcl_lib", function() |
| 7 | + describe("run", function() |
| 8 | + it("should take a single path, run it, and return the result", function() |
| 9 | + local expected = [[app: |
| 10 | + replicas: 2]] |
| 11 | + local result = assert(kcl_lib.run("./spec/test_data/schema.k")) |
| 12 | + assert.are.equal(expected, result.yaml_result) |
| 13 | + end) |
| 14 | + |
| 15 | + it( |
| 16 | + "should take an array of paths, run them, and return the result", |
| 17 | + function() |
| 18 | + local expected = [[app: |
| 19 | + replicas: 2 |
| 20 | +app2: |
| 21 | + replicas: 4]] |
| 22 | + local result = assert(kcl_lib.run({ |
| 23 | + "./spec/test_data/schema.k", |
| 24 | + "./spec/test_data/data.k", |
| 25 | + })) |
| 26 | + assert.are.equal(expected, result.yaml_result) |
| 27 | + end |
| 28 | + ) |
| 29 | + end) |
| 30 | + |
| 31 | + describe("format", function() |
| 32 | + local unformated_file = "/tmp/unformated.k" |
| 33 | + local unformated_content = [[ |
| 34 | +schema AppConfig: |
| 35 | + replicas: int |
| 36 | +
|
| 37 | +app: AppConfig { |
| 38 | + replicas: 2 |
| 39 | +}]] |
| 40 | + local expected_content = [[schema AppConfig: |
| 41 | + replicas: int |
| 42 | +
|
| 43 | +app: AppConfig { |
| 44 | + replicas: 2 |
| 45 | +} |
| 46 | +]] |
| 47 | + |
| 48 | + it( |
| 49 | + "should take a single path to unformated code, properly format it, and the path", |
| 50 | + function() |
| 51 | + local file = assert( |
| 52 | + io.open(unformated_file, "w"), |
| 53 | + "failed to open test file for formatting" |
| 54 | + ) |
| 55 | + file:write(unformated_content) |
| 56 | + file:close() |
| 57 | + local result = assert(kcl_lib.format(unformated_file)) |
| 58 | + assert.are.same({ unformated_file }, result) |
| 59 | + file = assert( |
| 60 | + io.open(unformated_file, "r"), |
| 61 | + "failed to open formatted file for reading" |
| 62 | + ) |
| 63 | + local data = file:read("*a") |
| 64 | + file:close() |
| 65 | + assert.are.equal(expected_content, data) |
| 66 | + os.execute("rm " .. unformated_file) |
| 67 | + end |
| 68 | + ) |
| 69 | + |
| 70 | + it( |
| 71 | + "should take a single path to formated code, do nothing, and return an empty table", |
| 72 | + function() |
| 73 | + local file = assert( |
| 74 | + io.open(unformated_file, "w"), |
| 75 | + "failed to open test file for formatting" |
| 76 | + ) |
| 77 | + file:write(expected_content) |
| 78 | + file:close() |
| 79 | + local result = assert(kcl_lib.format(unformated_file)) |
| 80 | + assert.are.same({}, result) |
| 81 | + os.execute("rm " .. unformated_file) |
| 82 | + end |
| 83 | + ) |
| 84 | + end) |
| 85 | +end) |
0 commit comments