Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. 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 | package com.google.devtools.build.lib.runtime; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
pcloudy | bdbf640 | 2019-02-28 08:27:36 -0800 | [diff] [blame] | 17 | import static java.util.Collections.singletonMap; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 18 | |
ishikhman | 1697219 | 2019-03-04 02:01:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.BlazeVersionInfo; |
| 21 | import com.google.devtools.build.lib.util.StringUtilities; |
pcloudy | bdbf640 | 2019-02-28 08:27:36 -0800 | [diff] [blame] | 22 | import java.util.Collections; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.TreeMap; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Tests {@link BlazeVersionInfo}. |
| 32 | */ |
| 33 | @RunWith(JUnit4.class) |
| 34 | public class BlazeVersionInfoTest { |
| 35 | |
| 36 | @Test |
| 37 | public void testEmptyVersionInfoMeansNotAvailable() { |
pcloudy | bdbf640 | 2019-02-28 08:27:36 -0800 | [diff] [blame] | 38 | BlazeVersionInfo info = new BlazeVersionInfo(Collections.<String, String>emptyMap()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 39 | assertThat(info.isAvailable()).isFalse(); |
| 40 | assertThat(info.getSummary()).isNull(); |
| 41 | assertThat(info.getReleaseName()).isEqualTo("development version"); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testReleaseNameIsDevelopmentIfBuildLabelIsNull() { |
pcloudy | bdbf640 | 2019-02-28 08:27:36 -0800 | [diff] [blame] | 46 | Map<String, String> data = singletonMap("Build label", ""); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 47 | BlazeVersionInfo info = new BlazeVersionInfo(data); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 48 | assertThat(info.getReleaseName()).isEqualTo("development version"); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testReleaseNameIfBuildLabelIsPresent() { |
pcloudy | bdbf640 | 2019-02-28 08:27:36 -0800 | [diff] [blame] | 53 | Map<String, String> data = singletonMap("Build label", "3/4/2009 (gold)"); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 54 | BlazeVersionInfo info = new BlazeVersionInfo(data); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 55 | assertThat(info.getReleaseName()).isEqualTo("release 3/4/2009 (gold)"); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testFancySummaryFormatting() { |
| 60 | Map<String, String> data = new HashMap<>(); |
| 61 | data.put("Some entry", "foo"); |
| 62 | data.put("Another entry", "bar"); |
| 63 | data.put("And a third entry", "baz"); |
| 64 | BlazeVersionInfo info = new BlazeVersionInfo(data); |
| 65 | Map<String, String> sortedData = new TreeMap<>(data); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 66 | assertThat(info.getSummary()).isEqualTo(StringUtilities.layoutTable(sortedData)); |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 67 | } |
ishikhman | 1697219 | 2019-03-04 02:01:37 -0800 | [diff] [blame] | 68 | |
| 69 | @Test |
| 70 | public void testVersionIsHeadIfBuildLabelIsNull() { |
| 71 | BlazeVersionInfo info = new BlazeVersionInfo(ImmutableMap.of()); |
| 72 | assertThat(info.getVersion()).isEmpty(); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testVersionsIIfBuildLabelIsPresent() { |
| 77 | Map<String, String> data = ImmutableMap.of("Build label", "123.4"); |
| 78 | BlazeVersionInfo info = new BlazeVersionInfo(data); |
| 79 | assertThat(info.getVersion()).isEqualTo("123.4"); |
| 80 | } |
Kristina Chodorow | aa8b152 | 2015-10-12 14:43:05 +0000 | [diff] [blame] | 81 | } |