|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.oap.server.library.it; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +/** |
| 29 | + * ITVersions is a utility class to read the version information from the environment file. |
| 30 | + */ |
| 31 | +public class ITVersions { |
| 32 | + |
| 33 | + static Map<String, String> ENV = readingEnv("test", "e2e-v2", "script", "env"); |
| 34 | + |
| 35 | + // Get the version from the environment file. |
| 36 | + public static String get(String key) { |
| 37 | + return ENV.get(key); |
| 38 | + } |
| 39 | + |
| 40 | + private static Map<String, String> readingEnv(String... envFile) { |
| 41 | + // Find the project root directory and get the environment file path. |
| 42 | + Path envFilePath = getProjectRootDir(); |
| 43 | + for (String subPath : envFile) { |
| 44 | + envFilePath = envFilePath.resolve(subPath); |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + return Files.readAllLines(envFilePath).stream() |
| 49 | + .filter(l -> !l.startsWith("#")) |
| 50 | + .map(l -> l.split("=", 2)) |
| 51 | + .filter(l -> l.length == 2) |
| 52 | + .collect(Collectors.toMap(l -> l[0], l -> l[1], (older, newer) -> newer)); |
| 53 | + } catch (IOException e) { |
| 54 | + throw new IllegalStateException("Reading environment file error, path: " + envFile, e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Get the project root directory which should contain the mvnw file. |
| 59 | + private static Path getProjectRootDir() { |
| 60 | + Path path = Paths.get(System.getProperty("user.dir")); |
| 61 | + while (!Files.exists(path.resolve("mvnw"))) { |
| 62 | + path = path.getParent(); |
| 63 | + } |
| 64 | + return path; |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments