Build Version as part of common, and add tests
Previously, the Version resource used by Version.java was compiled directly into the war, rather than being included in the java_library alongside the class that used it. The intended effect was to produce a version of "(dev)" when running from Eclipse, and an accurate version when running from the war. However, it made the build graph a little confusing, and moreover made it impossible to write a test that tests the actual format of the "git describe" output. Rearrange the build rules to include Version in the common:server library. This actually still results in "(dev)" when run from Eclipse, because there is no jar for this library in the Eclipse classpath, so looking up the resource will still fail as expected. Change-Id: I26e38a8c37aef9e1872faed1ab40d003e6dea946
This commit is contained in:
		
							
								
								
									
										63
									
								
								javatests/com/google/gerrit/common/VersionTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								javatests/com/google/gerrit/common/VersionTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
// Copyright (C) 2017 The Android Open Source Project
 | 
			
		||||
//
 | 
			
		||||
// 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.gerrit.common;
 | 
			
		||||
 | 
			
		||||
import static com.google.common.truth.Truth.assertThat;
 | 
			
		||||
 | 
			
		||||
import com.google.common.collect.ImmutableList;
 | 
			
		||||
import com.google.gerrit.launcher.GerritLauncher;
 | 
			
		||||
import java.util.regex.Pattern;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
public final class VersionTest {
 | 
			
		||||
  private static final Pattern DEV_PATTERN =
 | 
			
		||||
      Pattern.compile("^" + Pattern.quote(Version.DEV) + "$");
 | 
			
		||||
 | 
			
		||||
  private static final Pattern GIT_DESCRIBE_PATTERN =
 | 
			
		||||
      Pattern.compile(
 | 
			
		||||
          "^[1-9]+\\.[0-9]+(\\.[0-9]+)*(-rc[0-9]+)?(-[0-9]+" + "-g[0-9a-f]{7,})?(-dirty)?$");
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void version() {
 | 
			
		||||
    Pattern expected =
 | 
			
		||||
        GerritLauncher.isRunningInEclipse()
 | 
			
		||||
            ? DEV_PATTERN // Different source line so it shows up in coverage.
 | 
			
		||||
            : GIT_DESCRIBE_PATTERN;
 | 
			
		||||
    assertThat(Version.getVersion()).matches(expected);
 | 
			
		||||
    // Try again in case of caching issues.
 | 
			
		||||
    assertThat(Version.getVersion()).matches(expected);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void gitDescribePattern() {
 | 
			
		||||
    for (String suffix : ImmutableList.of("", "-dirty")) {
 | 
			
		||||
      assertThat("2.15-rc0" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15-rc0" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15-rc1" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15.1" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15.1.2" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15.1.2.3" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15.1-rc1" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15-rc2-123-gabcd123" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
      assertThat("2.15-123-gabcd123" + suffix).matches(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assertThat("2.15-ugly").doesNotMatch(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
    assertThat("(dev)").doesNotMatch(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
    assertThat("1").doesNotMatch(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
    assertThat("v2.15").doesNotMatch(GIT_DESCRIBE_PATTERN);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user