aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/rules/objc/IosDeviceTest.java
blob: 49b7540b6980666664ad19e58c9bdf495ea11b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.rules.objc;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.rules.apple.AppleCommandLineOptions;
import com.google.devtools.build.lib.rules.apple.DottedVersion;
import com.google.devtools.build.lib.rules.apple.XcodeVersionProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Unit tests for the {@code ios_device} rule.
 */
@RunWith(JUnit4.class)
public class IosDeviceTest extends BuildViewTestCase {

  @Test
  public void testIosVersion_specified() throws Exception {
    scratch.file("test/BUILD",
        "ios_device(name = 'foo', ios_version = '42.0', type = 'IPHONE_6',)");

    assertIosVersion("//test:foo", "42.0");
  }

  @Test
  public void testIosVersion_default() throws Exception {
    scratch.file("test/BUILD",
        "ios_device(name = 'foo', type = 'IPHONE_6',)");

    assertIosVersion("//test:foo", AppleCommandLineOptions.DEFAULT_IOS_SDK_VERSION);
  }

  @Test
  public void testIosVersion_flagValue() throws Exception {
    scratch.file("test/BUILD",
        "ios_device(name = 'foo', type = 'IPHONE_6',)");
    useConfiguration("--ios_sdk_version=42.3");

    assertIosVersion("//test:foo", "42.3");
  }

  /**
   * Tests that if {@code ios_device} specifies an xcode version that does not specify a {@code
   * default_ios_sdk_version}, the ios sdk version of the device defaults to the default value of
   * {@code default_ios_sdk_version} instead of the build configuration value. This is a confusing
   * (perhaps convoluted) corner case.
   */
  @Test
  public void testXcodeVersion_noIosVersion() throws Exception {
    scratch.file("test/BUILD",
        "xcode_version(name = 'my_xcode', version = '15.2')",
        "ios_device(name = 'foo', type = 'IPHONE_6', xcode = ':my_xcode')");
    useConfiguration("--xcode_version=2.1", "--ios_sdk_version=42.3");

    assertXcodeVersion("//test:foo", "15.2");
    assertIosVersion("//test:foo", XcodeVersionProperties.DEFAULT_IOS_SDK_VERSION);
  }

  @Test
  public void testXcodeVersion_withIosVersion() throws Exception {
    scratch.file("test/BUILD",
        "xcode_version(name = 'my_xcode', version = '15.2', default_ios_sdk_version='17.8')",
        "ios_device(name = 'foo', type = 'IPHONE_6', xcode = ':my_xcode')");
    useConfiguration("--xcode_version=2.1", "--ios_sdk_version=42.3");

    assertXcodeVersion("//test:foo", "15.2");
    assertIosVersion("//test:foo", "17.8");
  }

  @Test
  public void testXcodeVersion_iosVersionOverride() throws Exception {
    scratch.file("test/BUILD",
        "xcode_version(name = 'my_xcode', version = '15.2', default_ios_sdk_version='17.8')",
        "ios_device(name = 'foo', type = 'IPHONE_6', ios_version='98.7', xcode = ':my_xcode')");
    useConfiguration("--xcode_version=2.1", "--ios_sdk_version=42.3");

    assertXcodeVersion("//test:foo", "15.2");
    assertIosVersion("//test:foo", "98.7");
  }

  @Test
  public void testType() throws Exception {
    scratch.file("test/BUILD",
        "ios_device(name = 'foo', type = 'IPHONE_6',)");

    assertThat(view.hasErrors(getConfiguredTarget("//test:foo"))).isFalse();

    ConfiguredTarget target = getConfiguredTarget("//test:foo");
    IosDeviceProvider provider =
        (IosDeviceProvider) target.get(IosDeviceProvider.SKYLARK_CONSTRUCTOR.getKey());
    assertThat(provider.getType()).isEqualTo("IPHONE_6");
  }

  @Test
  public void testIosDeviceAttributesCanBeReadFromSkylark() throws Exception {
    scratch.file("examples/rule/BUILD");
    scratch.file(
        "examples/rule/apple_rules.bzl",
        "def my_rule_impl(ctx):",
        "   ios_device_attr = ctx.attr.ios_device[apple_common.IosDevice]",
        "   return struct(",
        "      xcode_version=ios_device_attr.xcode_version,",
        "      ios_version=ios_device_attr.ios_version,",
        "      type=ios_device_attr.type",
        "   )",
        "my_rule = rule(implementation = my_rule_impl,",
        "   attrs = {",
        "     'ios_device': attr.label(),",
        "   },",
        ")");
    scratch.file(
        "examples/apple_skylark/BUILD",
        "package(default_visibility = ['//visibility:public'])",
        "load('/examples/rule/apple_rules', 'my_rule')",
        "my_rule(",
        "    name = 'my_target',",
        "    ios_device = ':my_device',",
        ")",
        "ios_device(",
        "   name = 'my_device',",
        "   type = 'IPHONE_8',",
        "   xcode = ':my_xcode',",
        "   ios_version='98.7'",
        ")",
        "xcode_version(",
        "   name = 'my_xcode',",
        "   version = '15.2'",
        ")");

    RuleConfiguredTarget skylarkTarget =
        (RuleConfiguredTarget) getConfiguredTarget("//examples/apple_skylark:my_target");
    assertThat((String) skylarkTarget.get("xcode_version")).isEqualTo("15.2");
    assertThat((String) skylarkTarget.get("type")).isEqualTo("IPHONE_8");
    assertThat((String) skylarkTarget.get("ios_version")).isEqualTo("98.7");
  }

  private void assertXcodeVersion(String label, String version) throws Exception {
    assertThat(view.hasErrors(getConfiguredTarget(label))).isFalse();

    ConfiguredTarget target = getConfiguredTarget(label);
    IosDeviceProvider provider =
        (IosDeviceProvider) target.get(IosDeviceProvider.SKYLARK_CONSTRUCTOR.getKey());
    assertThat(provider.getXcodeVersion()).isEqualTo(DottedVersion.fromString(version));
  }

  private void assertIosVersion(String label, String version) throws Exception {
    assertThat(view.hasErrors(getConfiguredTarget(label))).isFalse();

    ConfiguredTarget target = getConfiguredTarget(label);
    IosDeviceProvider provider =
        (IosDeviceProvider) target.get(IosDeviceProvider.SKYLARK_CONSTRUCTOR.getKey());
    assertThat(provider.getIosVersion()).isEqualTo(DottedVersion.fromString(version));
  }
}