|
| 1 | +%% |
| 2 | +%% Copyright (c) 2025 Winford (UncleGrumpy) <winford@object.stream> |
| 3 | +%% All rights reserved. |
| 4 | +%% |
| 5 | +%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +%% you may not use this file except in compliance with the License. |
| 7 | +%% You may obtain a copy of the License at |
| 8 | +%% |
| 9 | +%% http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +%% |
| 11 | +%% Unless required by applicable law or agreed to in writing, software |
| 12 | +%% distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +%% See the License for the specific language governing permissions and |
| 15 | +%% limitations under the License. |
| 16 | +%% |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | +-module(uf2create_tests). |
| 21 | + |
| 22 | +-export([run/1]). |
| 23 | + |
| 24 | +run(Opts) -> |
| 25 | + ok = test_defaults(Opts), |
| 26 | + ok = test_flags(Opts), |
| 27 | + ok = test_rebar_overrides(Opts), |
| 28 | + ok. |
| 29 | + |
| 30 | +%% @private |
| 31 | +test_defaults(Opts) -> |
| 32 | + AppsDir = maps:get(apps_dir, Opts), |
| 33 | + AppDir = test:make_path([AppsDir, "myapp"]), |
| 34 | + UF2Path = test:make_path([AppDir, "_build/default/lib/myapp.uf2"]), |
| 35 | + case test:file_exists(UF2Path) of |
| 36 | + ok -> |
| 37 | + Del = lists:join(" ", ["rm -v", UF2Path]), |
| 38 | + os:cmd(Del); |
| 39 | + _ -> |
| 40 | + ok |
| 41 | + end, |
| 42 | + |
| 43 | + Cmd = create_uf2create_cmd(AppDir, [], []), |
| 44 | + Output = test:execute_cmd(Cmd, Opts), |
| 45 | + test:debug(Output, Opts), |
| 46 | + |
| 47 | + ok = test:expect_contains("UF2 file written to", Output), |
| 48 | + ok = test:expect_contains("_build/default/lib/myapp.uf2", Output), |
| 49 | + ok = test:file_exists(UF2Path), |
| 50 | + |
| 51 | + test:tick(). |
| 52 | + |
| 53 | +test_flags(Opts) -> |
| 54 | + test_flags(Opts, [], [ |
| 55 | + {"--start", "0x10180000"}, |
| 56 | + {"--family_id", "universal"}, |
| 57 | + {"--output", "_build/default/lib/myapp.uf2"}, |
| 58 | + {"--input", "_build/default/lib/myapp.avm"} |
| 59 | + ]), |
| 60 | + test_flags(Opts, [{"-s", "0x12345"}, {"-f", "rp2040"}], [ |
| 61 | + {"--start", "0x12345"}, |
| 62 | + {"--family_id", "rp2040"} |
| 63 | + ]), |
| 64 | + test_flags(Opts, [{"--family_id", "rp2350"}], [ |
| 65 | + {"--family_id", "data"} |
| 66 | + ]), |
| 67 | + ok. |
| 68 | + |
| 69 | +test_flags(Opts, Flags, FlagExpectList) -> |
| 70 | + AppsDir = maps:get(apps_dir, Opts), |
| 71 | + AppDir = test:make_path([AppsDir, "myapp"]), |
| 72 | + UF2Path = test:make_path([AppDir, "_build/default/lib/myapp.uf2"]), |
| 73 | + case test:file_exists(UF2Path) of |
| 74 | + ok -> |
| 75 | + Del = lists:join(" ", ["rm -v", UF2Path]), |
| 76 | + os:cmd(Del); |
| 77 | + _ -> |
| 78 | + ok |
| 79 | + end, |
| 80 | + |
| 81 | + Cmd = create_uf2create_cmd(AppDir, Flags, []), |
| 82 | + Output = test:execute_cmd(Cmd, Opts), |
| 83 | + test:debug(Output, Opts), |
| 84 | + |
| 85 | + lists:foreach( |
| 86 | + fun({Flag, Value}) -> |
| 87 | + test:expect_contains(io_lib:format("~s ~s", [Flag, Value]), Output) |
| 88 | + end, |
| 89 | + FlagExpectList |
| 90 | + ), |
| 91 | + ok = test:expect_contains("UF2 file written to", Output), |
| 92 | + ok = test:expect_contains("_build/default/lib/myapp.uf2", Output), |
| 93 | + ok = test:file_exists(UF2Path), |
| 94 | + |
| 95 | + test:tick(). |
| 96 | + |
| 97 | +%% @private |
| 98 | +test_rebar_overrides(Opts) -> |
| 99 | + %% the rebar_overrides rebar.config specifies start address "0x10180800" |
| 100 | + test_rebar_overrides( |
| 101 | + Opts, [], "ATOMVM_PICO_APP_START", "0xDEADBEEF", "--start", "0x10180800" |
| 102 | + ), |
| 103 | + |
| 104 | + test_rebar_overrides( |
| 105 | + Opts, [{"-s", "0x123456"}], "ATOMVM_PICO_APP_START", "0xDEADBEEF", "--start", "0x123456" |
| 106 | + ), |
| 107 | + |
| 108 | + %% the rebar_overrides rebar.config specifies family_id "rp2350" which is 'data' |
| 109 | + test_rebar_overrides( |
| 110 | + Opts, [], "ATOMVM_PICO_UF2_FAMILY", "rp2040", "--family_id", "data" |
| 111 | + ), |
| 112 | + |
| 113 | + test_rebar_overrides( |
| 114 | + Opts, [{"-f", "universal"}], "ATOMVM_PICO_UF2_FAMILY", "rp2040", "--family_id", "universal" |
| 115 | + ), |
| 116 | + |
| 117 | + ok. |
| 118 | + |
| 119 | +%% @private |
| 120 | +test_rebar_overrides(Opts, Flags, EnvVar, Value, Flag, ExpectedValue) -> |
| 121 | + AppsDir = maps:get(apps_dir, Opts), |
| 122 | + AppDir = test:make_path([AppsDir, "rebar_overrides"]), |
| 123 | + UF2Path = test:make_path([AppDir, "_build/default/lib/myapp.uf2"]), |
| 124 | + case test:file_exists(UF2Path) of |
| 125 | + ok -> |
| 126 | + Del = lists:join(" ", ["rm -v", UF2Path]), |
| 127 | + os:cmd(Del); |
| 128 | + _ -> |
| 129 | + ok |
| 130 | + end, |
| 131 | + |
| 132 | + Cmd = create_uf2create_cmd(AppDir, Flags, [{EnvVar, Value}]), |
| 133 | + Output = test:execute_cmd(Cmd, Opts), |
| 134 | + test:debug(Output, Opts), |
| 135 | + |
| 136 | + ok = test:expect_contains(io_lib:format("~s ~s", [Flag, ExpectedValue]), Output), |
| 137 | + ok = test:expect_contains("UF2 file written to", Output), |
| 138 | + ok = test:expect_contains("_build/default/lib/myapp.uf2", Output), |
| 139 | + ok = test:file_exists(UF2Path), |
| 140 | + |
| 141 | + test:tick(). |
| 142 | + |
| 143 | +%% @private |
| 144 | +create_uf2create_cmd(AppDir, Opts, Env) -> |
| 145 | + test:create_rebar3_cmd(AppDir, uf2create, Opts, Env). |
0 commit comments