1# Copyright 2022 Google LLC. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the License); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""unittests""" 16 17load("//:visibility.bzl", "RULES_KOTLIN") 18load("//kotlin/common/testing:unittest_suites.bzl", "kt_unittest_suites") 19load("//kotlin/jvm/util:file_factory.bzl", "FileFactory") 20load("@bazel_skylib//lib:unittest.bzl", "asserts") 21 22visibility(RULES_KOTLIN) 23 24unittests = kt_unittest_suites.create() 25 26def _base_from_file(ctx, env): 27 base_file = ctx.actions.declare_file("file/base.txt") 28 factory = FileFactory(ctx, base_file) 29 30 _assert_path_equals(ctx, env, "/file/base", factory.base_as_path) 31 32 return [base_file] 33 34unittests.expect_finish(_base_from_file) 35 36def _declare(ctx, env): 37 factory = FileFactory(ctx, "string/base") 38 39 _assert_path_equals(ctx, env, "/string/base", factory.base_as_path) 40 41 a_file = factory.declare_file("a.txt") 42 _assert_path_equals(ctx, env, "/string/basea.txt", a_file.path) 43 44 b_dir = factory.declare_directory("b_dir") 45 _assert_path_equals(ctx, env, "/string/baseb_dir", b_dir.path) 46 47 return [a_file, b_dir] 48 49unittests.expect_finish(_declare) 50 51def _derive(ctx, env): 52 factory = FileFactory(ctx, "") 53 54 # Once 55 factory_once = factory.derive("once") 56 _assert_path_equals(ctx, env, "/once", factory_once.base_as_path) 57 58 # Twice 59 factory_twice = factory_once.derive("/twice") 60 _assert_path_equals(ctx, env, "/once/twice", factory_twice.base_as_path) 61 62unittests.expect_finish(_derive) 63 64def _base_file_without_extension(ctx): 65 base_file = ctx.actions.declare_file(ctx.label.name + "/BUILD") 66 FileFactory(ctx, base_file) 67 68unittests.expect_fail(_base_file_without_extension, "file must have an extension") 69 70def _base_file_from_different_pkg(ctx): 71 mock_file = struct(owner = struct(package = ctx.label.package + "/sub"), extension = "txt") 72 FileFactory(ctx, mock_file) 73 74unittests.expect_fail(_base_file_from_different_pkg, "file must be from ctx package") 75 76def _assert_path_equals(ctx, env, expected, actual): 77 pkg_path = ctx.bin_dir.path + "/" + ctx.label.package 78 79 asserts.equals( 80 env, 81 pkg_path + expected, 82 actual, 83 ) 84 85_test, _fail = unittests.close() # @unused 86