Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions ci/check-environment-variable-names.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,63 @@ Describe 'the check over a repository' {
Remove-Item -Recurse -Force $repo
}
}

It 'passes an old name in a file that reads it as a deprecated fallback' {
$repo = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid())
New-Item -ItemType Directory $repo | Out-Null
try {
git -C $repo init -q
$file = Join-Path $repo 'pipeline/pipeline.did/src/main/java/fiftyone/pipeline/did/DidClient.java'
New-Item -ItemType Directory (Split-Path $file) -Force | Out-Null
Set-Content -LiteralPath $file `
-Value 'String ENDPOINT_VARIABLE = "FOD_CLOUD_API_URL";'
git -C $repo add -A
{ & $script:Check -RepoRoot $repo } | Should -Not -Throw
} finally {
Remove-Item -Recurse -Force $repo
}
}

It 'fails on the same old name in a file that is not named for it' {
$repo = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid())
New-Item -ItemType Directory $repo | Out-Null
try {
git -C $repo init -q
Set-Content -LiteralPath (Join-Path $repo 'Other.java') `
-Value 'String ENDPOINT_VARIABLE = "FOD_CLOUD_API_URL";'
git -C $repo add -A
{ & $script:Check -RepoRoot $repo } |
Should -Throw '*and 1 old names are read*'
} finally {
Remove-Item -Recurse -Force $repo
}
}
}

Describe 'Test-DeprecatedFallback' {

# An old name is allowed only in a file that reads it as a deprecated
# fallback or tests that read, and only the names listed for that file.
It 'allows <Value> in <Path>' -ForEach @(
@{ Path = 'pipeline/pipeline.did/src/main/java/fiftyone/pipeline/did/DidClient.java'; Value = '"FOD_CLOUD_API_URL' }
@{ Path = 'pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java'; Value = '"RESOURCE_KEY' }
@{ Path = 'pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java'; Value = '"LICENSE_KEY' }
@{ Path = 'ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/testhelpers/FileUtils.java'; Value = '51DEGREES_IPI_PATH' }
@{ Path = 'examples/device-detection/web/getting-started.onprem/src/main/webapp/WEB-INF/51Degrees-OnPrem.xml'; Value = '51DEGREES_DD_PATH' }
) {
Test-DeprecatedFallback $Path $Value | Should -BeTrue
}

It 'refuses <Value> in <Path>' -ForEach @(
@{ Path = 'pipeline/pipeline.core/src/main/java/Any.java'; Value = '"FOD_CLOUD_API_URL' }
@{ Path = 'ci/run-unit-tests.ps1'; Value = 'env:RESOURCE_KEY' }
@{ Path = 'pipeline/pipeline.did/src/main/java/fiftyone/pipeline/did/DidClient.java'; Value = '"RESOURCE_KEY' }
@{ Path = 'pipeline/pipeline.did/src/main/java/fiftyone/pipeline/did/DidClient.java'; Value = 'LEGACY_ENV_VARS' }
@{ Path = 'pipeline/pipeline.did/src/test/java/fiftyone/pipeline/did/LiveCloudKeys.java'; Value = '"FOD_CLOUD_API_URL' }
@{ Path = 'ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/testhelpers/FileUtils.java'; Value = '51DEGREES_DD_PATH' }
@{ Path = 'ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/testhelpers/FileUtils.java'; Value = '51degrees_ipi_path' }
@{ Path = 'examples/device-detection/web/getting-started.onprem/src/main/webapp/WEB-INF/51Degrees-OnPrem.xml'; Value = '51DEGREES_IPI_PATH' }
) {
Test-DeprecatedFallback $Path $Value | Should -BeFalse
}
}
91 changes: 85 additions & 6 deletions ci/check-environment-variable-names.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Every 51Degrees environment variable name in this repository starts with an
# underscore, because a POSIX shell cannot set a name that starts with a digit,
# and each variable has one name with nothing read in its place. This reads
# every file git tracks, which leaves out build output and the submodules, and
# fails on any name that starts with the digit and on any old name that is
# still read.
# and each variable has one name. An old name is still read, as a deprecated
# fallback for when its new name is not set, by the files listed in
# DeprecatedFallbacks below and by nothing else. This reads every file git
# tracks, which leaves out build output and the submodules, and fails on any
# name that starts with the digit and on any old name, outside the files
# listed for that name.
#
# Dot-sourcing the script defines Find-EnvironmentNameProblem without reading
# the repository, which is how its tests use it.
# Dot-sourcing the script defines Find-EnvironmentNameProblem and
# Test-DeprecatedFallback without reading the repository, which is how its
# tests use them.

[CmdletBinding()]
param(
Expand Down Expand Up @@ -53,6 +56,79 @@ $script:FallbackPattern = [regex]::new(
'(?:RESOURCE_KEY|LICENSE_KEY|FOD_CLOUD_API_URL)(?![A-Za-z0-9_])|' +
'\bLEGACY_[A-Z_]*ENV_VARS?\b')

# The files that read an old name as a deprecated fallback, or that test
# that read, each with the old names it may hold. The names are put together
# from parts so that this file does not match itself.
$oldEndpoint = 'FOD_CLOUD' + '_API_URL'
$oldResourceKey = 'RESOURCE' + '_KEY'
$oldLicenseKey = 'LICENSE' + '_KEY'
$oldDigitKey = $digitInitial + $oldResourceKey
$oldIpiPath = $digitInitial + 'IPI_PATH'
$oldDdPath = $digitInitial + 'DD_PATH'
$cloudEngine = 'pipeline/pipeline.cloudrequestengine/src'
$did = 'pipeline/pipeline.did/src'
$examples = 'pipeline/pipeline.developer-examples/pipeline.developer-examples'
$ipiShared = 'ip-intelligence/ip-intelligence.shared/src'
$ipiHelpers = 'fiftyone/ipintelligence/shared/testhelpers'
$ipiExamples = 'examples/ip-intelligence/shared/src'
$ipiExampleHelpers = 'fiftyone/ipintelligence/examples/shared'
$onPrem = 'examples/device-detection/web/getting-started.onprem/src'
$onPremPackage = 'fiftyone/devicedetection/examples/web'
$script:DeprecatedFallbacks = @{
"$cloudEngine/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/Constants.java" =
@($oldEndpoint)
"$cloudEngine/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilderTests.java" =
@($oldEndpoint)
"$did/main/java/fiftyone/pipeline/did/DidClient.java" =
@($oldEndpoint)
"$did/test/java/fiftyone/pipeline/did/DidClientEnvironmentTests.java" =
@($oldEndpoint)
"$did/test/java/fiftyone/pipeline/did/LiveCloudKeys.java" =
@($oldResourceKey, $oldLicenseKey)
"$did/test/java/fiftyone/pipeline/did/LiveCloudKeysTests.java" =
@($oldResourceKey, $oldLicenseKey)
"$examples.cloud-engine/src/main/java/pipeline/developerexamples/cloudengine/Main.java" =
@($oldEndpoint, $oldResourceKey)
"$examples.cloud-engine/src/test/java/pipeline/developerexamples/cloudengine/EndpointTests.java" =
@($oldEndpoint, $oldResourceKey)
"$examples.fodid/src/main/java/pipeline/developerexamples/fodid/CreatorContextDemoServer.java" =
@($oldResourceKey, $oldLicenseKey)
"$examples.fodid/src/test/java/pipeline/developerexamples/fodid/DeprecatedKeyNameTests.java" =
@($oldResourceKey, $oldLicenseKey)
"$ipiShared/main/java/$ipiHelpers/FileUtils.java" = @($oldIpiPath)
"$ipiShared/test/java/$ipiHelpers/FileUtilsTest.java" = @($oldIpiPath)
"$ipiShared/main/java/$ipiHelpers/KeyUtils.java" = @($oldDigitKey)
"$ipiShared/test/java/$ipiHelpers/KeyUtilsTest.java" = @($oldDigitKey)
"$ipiExamples/main/java/$ipiExampleHelpers/DataFileHelper.java" =
@($oldIpiPath)
"$ipiExamples/test/java/$ipiExampleHelpers/DataFileHelperTest.java" =
@($oldIpiPath)
"$ipiExamples/main/java/$ipiExampleHelpers/KeyHelper.java" =
@($oldDigitKey)
"$ipiExamples/test/java/$ipiExampleHelpers/KeyHelperTest.java" =
@($oldDigitKey)
'ip-intelligence/ip-intelligence.translation/src/test/java/fiftyone/ipintelligence/translation/CountryTranslationIntegrationTest.java' =
@($oldIpiPath)
"$onPrem/main/webapp/WEB-INF/51Degrees-OnPrem.xml" = @($oldDdPath)
"$onPrem/main/java/$onPremPackage/GettingStartedWebOnPrem.java" =
@($oldDdPath)
"$onPrem/test/java/$onPremPackage/OnPremDataFileOptionTest.java" =
@($oldDdPath)
}

# Whether the text matched in the file at this path, relative to the root
# with forward slashes, is an old name that file is listed for. The name is
# the run of name characters the match ends with, compared in its exact
# case.
function Test-DeprecatedFallback([string]$Path, [string]$Value) {
$allowed = $script:DeprecatedFallbacks[$Path]
if ($null -eq $allowed) {
return $false
}
$name = [regex]::Match($Value, '[A-Za-z0-9_]+$').Value
return $allowed -ccontains $name
}

# The problems on one line, each as its kind, 'digit' or 'fallback', and the
# text matched.
function Find-EnvironmentNameProblem([string]$Line) {
Expand Down Expand Up @@ -99,6 +175,9 @@ foreach ($relative in $tracked) {
foreach ($line in [IO.File]::ReadLines($path)) {
$lineNumber++
foreach ($problem in Find-EnvironmentNameProblem $line) {
if (Test-DeprecatedFallback $relative $problem.Value) {
continue
}
$text = "${relative}:${lineNumber}: $($problem.Value)"
if ($problem.Kind -eq 'digit') {
$found += $text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,37 @@ public class GettingStartedWebOnPrem extends HttpServlet {
public static String resourceBase = "examples/device-detection/web/getting-started.onprem/src/main/webapp";
public static Logger logger = LoggerFactory.getLogger(GettingStartedWebOnPrem.class);

private static final String DD_PATH_VARIABLE = "_51DEGREES_DD_PATH";
private static final String OLD_DD_PATH_VARIABLE = "51DEGREES_DD_PATH";

/**
* Logs a warning when the configuration takes the data file from the
* deprecated name, which it does when that name is set and the name
* that replaces it is not.
*/
private static void warnOfOldDataFileName() {
if (isSet(OLD_DD_PATH_VARIABLE) && isSet(DD_PATH_VARIABLE) == false) {
logger.warn(
"The environment variable " + OLD_DD_PATH_VARIABLE
+ " is deprecated and " + DD_PATH_VARIABLE
+ " replaces it. Set " + DD_PATH_VARIABLE
+ " to the same value, as a later release may no longer "
+ "read " + OLD_DD_PATH_VARIABLE + ".");
}
}

private static boolean isSet(String name) {
String value = System.getenv(name);
if (value == null || value.isEmpty()) {
value = System.getProperty(name);
}
return value != null && value.isEmpty() == false;
}

public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
logger.info("Running Example {}", GettingStartedWebOnPrem.class);
warnOfOldDataFileName();

// If the data file the configuration names is not at that path, it
// is searched for, and the free Lite file is downloaded if missing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
<CreateTempDataCopy>false</CreateTempDataCopy>
<!--
The data file is taken from the _51DEGREES_DD_PATH environment
variable, then from TestDataFile (an environment variable or
system property), and otherwise the free Lite file is used.
variable, then from its deprecated name 51DEGREES_DD_PATH,
then from TestDataFile (an environment variable or system
property), and otherwise the free Lite file is used.
-->
<DataFile>${_51DEGREES_DD_PATH:-${TestDataFile:-cxx/data/device-detection/51Degrees-LiteV4.1.hash}}</DataFile>
<DataFile>${_51DEGREES_DD_PATH:-${51DEGREES_DD_PATH:-${TestDataFile:-cxx/data/device-detection/51Degrees-LiteV4.1.hash}}}</DataFile>
<PerformanceProfile>LowMemory</PerformanceProfile>
</BuildParameters>
<BuilderName>DeviceDetectionHashEngine</BuilderName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,35 @@
*/
public class OnPremDataFileOptionTest {
private static final String DD_PATH = "_51DEGREES_DD_PATH";
private static final String OLD_DD_PATH = "51DEGREES_DD_PATH";
private static final String TEST_DATA_FILE = "TestDataFile";
private static final String LITE =
"cxx/data/device-detection/51Degrees-LiteV4.1.hash";

private String savedDdPath;
private String savedOldDdPath;
private String savedTestDataFile;

@Before
public void saveProperties() {
// An environment variable of either name would take precedence over
// the system properties set here, so the test cannot run.
assumeTrue(System.getenv(DD_PATH) == null);
assumeTrue(System.getenv(OLD_DD_PATH) == null);
assumeTrue(System.getenv(TEST_DATA_FILE) == null);
assumeTrue(System.getenv(TEST_DATA_FILE.toUpperCase()) == null);
savedDdPath = System.getProperty(DD_PATH);
savedOldDdPath = System.getProperty(OLD_DD_PATH);
savedTestDataFile = System.getProperty(TEST_DATA_FILE);
System.clearProperty(DD_PATH);
System.clearProperty(OLD_DD_PATH);
System.clearProperty(TEST_DATA_FILE);
}

@After
public void restoreProperties() {
restore(DD_PATH, savedDdPath);
restore(OLD_DD_PATH, savedOldDdPath);
restore(TEST_DATA_FILE, savedTestDataFile);
}

Expand All @@ -85,6 +91,20 @@ public void prefersDdPathOverTestDataFile() throws Exception {
assertEquals("from-dd-path.hash", dataFile());
}

@Test
public void usesOldDdPathWhenNewIsNotSet() throws Exception {
System.setProperty(TEST_DATA_FILE, "from-test-data-file.hash");
System.setProperty(OLD_DD_PATH, "from-old-dd-path.hash");
assertEquals("from-old-dd-path.hash", dataFile());
}

@Test
public void prefersDdPathOverOldDdPath() throws Exception {
System.setProperty(OLD_DD_PATH, "from-old-dd-path.hash");
System.setProperty(DD_PATH, "from-dd-path.hash");
assertEquals("from-dd-path.hash", dataFile());
}

private static String dataFile() throws Exception {
PipelineOptions options = PipelineOptionsFactory.getOptionsFromFile(
getFilePath(resourceBase) + "/WEB-INF/51Degrees-OnPrem.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void run(String ddDataFile,
logger.error("Failed to find IP Intelligence data file at '{}'. " +
"Please provide a valid path to an IP Intelligence data file (.ipi). " +
"An explicit path can be supplied via the {} environment variable.",
ipiDataFile, DataFileHelper.IPI_PATH_ENV_VAR);
ipiDataFile, DataFileHelper.IPI_PATH_VARIABLE);
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void run(String dataFile,
"An explicit path can be supplied via the {} environment variable. " +
"For testing, you can obtain an enterprise data file by contacting us at " +
"https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-java&utm_content=console-src-main-java-fiftyone-ipintelligence-examples-console-gettingstartedonprem.java&utm_term=data-file-not-found",
dataFile, DataFileHelper.IPI_PATH_ENV_VAR);
dataFile, DataFileHelper.IPI_PATH_VARIABLE);
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void run(String dataFile, OutputStream output) throws Exception {
logger.error("Failed to find IP Intelligence data file at '{}'. " +
"Please provide a valid path to an IP Intelligence data file (.ipi). " +
"An explicit path can be supplied via the {} environment variable.",
dataFile, DataFileHelper.IPI_PATH_ENV_VAR);
dataFile, DataFileHelper.IPI_PATH_VARIABLE);
throw e;
}
// Build an on-premise IP Intelligence engine with the low memory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static void run(String dataFile, InputStream is, OutputStream os) throws
logger.error("Failed to find IP Intelligence data file at '{}'. " +
"Please provide a valid path to an IP Intelligence data file (.ipi). " +
"An explicit path can be supplied via the {} environment variable.",
dataFile, DataFileHelper.IPI_PATH_ENV_VAR);
dataFile, DataFileHelper.IPI_PATH_VARIABLE);
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,20 @@ private interface Main {
/**
* Runs the example's main with no arguments and the data file and
* licence key system properties removed, and returns what it wrote to
* standard output.
* standard output. The deprecated name of the data file variable is
* read when the current one is not set, so both are checked and
* removed.
*/
@SuppressWarnings("deprecation")
private static String runWithoutSettings(Main main) throws Exception {
assumeTrue("Skipping, a data file or licence key is set in the environment",
System.getenv(DataFileHelper.IPI_PATH_ENV_VAR) == null &&
System.getenv(DataFileHelper.IPI_PATH_VARIABLE) == null &&
System.getenv(DataFileHelper.IPI_PATH_ENV_VAR) == null &&
System.getenv(LICENSE_KEY) == null &&
System.getenv(LICENSE_KEY.toUpperCase()) == null);
String dataFile = System.clearProperty(DataFileHelper.IPI_PATH_ENV_VAR);
String dataFile = System.clearProperty(DataFileHelper.IPI_PATH_VARIABLE);
String oldNameDataFile =
System.clearProperty(DataFileHelper.IPI_PATH_ENV_VAR);
String licenseKey = System.clearProperty(LICENSE_KEY);
PrintStream out = System.out;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Expand All @@ -74,7 +80,11 @@ private static String runWithoutSettings(Main main) throws Exception {
} finally {
System.setOut(out);
if (dataFile != null) {
System.setProperty(DataFileHelper.IPI_PATH_ENV_VAR, dataFile);
System.setProperty(DataFileHelper.IPI_PATH_VARIABLE, dataFile);
}
if (oldNameDataFile != null) {
System.setProperty(DataFileHelper.IPI_PATH_ENV_VAR,
oldNameDataFile);
}
if (licenseKey != null) {
System.setProperty(LICENSE_KEY, licenseKey);
Expand Down
Loading
Loading