Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/2201.8.x' into fix-ballerina-l…
Browse files Browse the repository at this point in the history
…ang-iss-40597-8.x
  • Loading branch information
Thevakumar-Luheerathan committed Dec 15, 2023
2 parents 5b2c0de + d53600f commit 16c6d1a
Show file tree
Hide file tree
Showing 39 changed files with 534 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public class TableValueImpl<K, V> implements TableValue<K, V> {
private long maxIntKey = 0;

//These are required to achieve the iterator behavior
private LinkedHashMap<Long, Long> indexToKeyMap;
private LinkedHashMap<Long, K> indexToKeyMap;
private LinkedHashMap<Long, Long> keyToIndexMap;
private LinkedHashMap<Long, KeyValuePair<K, V>> keyValues;
private LinkedHashMap<K, V> keyValues;
private long noOfAddedEntries = 0;

private boolean nextKeySupported;
Expand Down Expand Up @@ -478,11 +478,9 @@ private class TableIterator<K, V> implements IteratorValue {

@Override
public Object next() {
Long hash = indexToKeyMap.get(cursor);
if (hash != null) {
KeyValuePair<K, V> keyValuePair = (KeyValuePair<K, V>) keyValues.get(hash);
K key = keyValuePair.getKey();
V value = keyValuePair.getValue();
K key = (K) indexToKeyMap.get(cursor);
if (key != null) {
V value = (V) keyValues.get(key);

List<Type> types = new ArrayList<>();
types.add(TypeChecker.getType(key));
Expand Down Expand Up @@ -536,7 +534,7 @@ public V putData(V data) {
entries.put(hash, entryList);
updateIndexKeyMappings((K) data, hash);
values.put(hash, newData);
keyValues.put(hash, KeyValuePair.of((K) data, data));
keyValues.put((K) data, data);
return data;
}

Expand Down Expand Up @@ -590,7 +588,7 @@ public void addData(V data) {
extEntries.add(entry);
List<V> extValues = values.get(hash);
extValues.add(data);
keyValues.put(hash, KeyValuePair.of(key, data));
keyValues.put(key, data);
updateIndexKeyMappings(key, hash);
return;
}
Expand Down Expand Up @@ -632,13 +630,14 @@ public V putData(K key, V data) {
}

private V putData(K key, V value, List<V> data, Map.Entry<K, V> entry, Long hash) {

List<Map.Entry<K, V>> entryList = new ArrayList<>();
entryList.add(entry);
entries.put(hash, entryList);
keys.put(hash, key);
updateIndexKeyMappings(key, hash);
values.put(hash, data);
keyValues.put(hash, KeyValuePair.of(key, value));
keyValues.put(key, value);
return data.get(0);
}

Expand All @@ -656,8 +655,8 @@ public V putData(V data) {
}

public V remove(K key) {
keyValues.remove(key);
Long hash = TableUtils.hash(key, null);
keyValues.remove(hash);
List<Map.Entry<K, V>> entryList = entries.get(hash);
if (entryList != null && entryList.size() > 1) {
for (Map.Entry<K, V> entry: entryList) {
Expand Down Expand Up @@ -748,33 +747,11 @@ public K wrapKey(MapValue data) {
}
}

private static final class KeyValuePair<K, V> {
private K key;
private V value;

public KeyValuePair(K key, V value) {
this.key = key;
this.value = value;
}

public static <K, V> KeyValuePair<K, V> of(K key, V value) {
return new KeyValuePair<>(key, value);
}

public K getKey() {
return key;
}

public V getValue() {
return value;
}
}

// This method updates the indexes and the order required by the iterators
private void updateIndexKeyMappings(K key, Long hash) {
if (!keyToIndexMap.containsKey(hash)) {
keyToIndexMap.put(hash, noOfAddedEntries);
indexToKeyMap.put(noOfAddedEntries, hash);
indexToKeyMap.put(noOfAddedEntries, key);
noOfAddedEntries++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static void startResourceObservation(Environment env, BString module, BSt
String serviceNameValue = serviceName.getValue();
String serviceNameSuffix = System.getenv(BAL_OBSERVE_SERVICE_NAME_SUFFIX);
if (serviceNameSuffix != null && !serviceNameSuffix.isEmpty()) {
observerContext.setServiceName(serviceNameValue + "-" + serviceNameSuffix);
serviceNameValue += "-" + serviceNameSuffix;
}

observerContext.setServiceName(serviceNameValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,26 @@ public static void generateTesterinaReports(Project project, TestReport testRepo
}
}

Path reportZipPath = Paths.get(System.getProperty(BALLERINA_HOME)).resolve(BALLERINA_HOME_LIB).
resolve(TesterinaConstants.TOOLS_DIR_NAME).resolve(TesterinaConstants.COVERAGE_DIR).
resolve(REPORT_ZIP_NAME);
// Dump the Testerina html report only if '--test-report' flag is provided
if (project.buildOptions().testReport()) {
Path reportZipPath = getReportToolsPath();
if (Files.exists(reportZipPath)) {
String content;
try {
try (FileInputStream fileInputStream = new FileInputStream(reportZipPath.toFile())) {
CodeCoverageUtils.unzipReportResources(fileInputStream,
reportDir.toFile());
CodeCoverageUtils.unzipReportResources(fileInputStream, reportDir.toFile());
}
content = Files.readString(reportDir.resolve(RESULTS_HTML_FILE));
content = content.replace(REPORT_DATA_PLACEHOLDER, json);
} catch (IOException e) {
throw createLauncherException("error occurred while preparing test report: " + e.toString());
throw createLauncherException("error occurred while preparing test report: " + e);
}
File htmlFile = new File(reportDir.resolve(RESULTS_HTML_FILE).toString());
try (FileOutputStream fileOutputStream = new FileOutputStream(htmlFile)) {
try (Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
writer.write(new String(content.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
out.println("\tView the test report at: " +
FILE_PROTOCOL + Paths.get(htmlFile.getPath()).toAbsolutePath().normalize().toString());
FILE_PROTOCOL + Paths.get(htmlFile.getPath()).toAbsolutePath().normalize());
}
}
} else {
Expand All @@ -210,6 +207,17 @@ public static void generateTesterinaReports(Project project, TestReport testRepo
}
}

/**
* Get the path of the report tools template from Ballerina home.
*
* @return path of the report tools template
*/
public static Path getReportToolsPath() {
return Paths.get(System.getProperty(BALLERINA_HOME)).resolve(BALLERINA_HOME_LIB).
resolve(TesterinaConstants.TOOLS_DIR_NAME).resolve(TesterinaConstants.COVERAGE_DIR).
resolve(REPORT_ZIP_NAME);
}

/**
* Loads the ModuleStatus object by reading a given Json.
*
Expand All @@ -235,7 +243,7 @@ public static void writeToTestSuiteJson(Map<String, TestSuite> testSuiteMap, Pat
try {
Files.createDirectories(testsCachePath);
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't create test cache directories : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't create test cache directories : " + e);
}
}

Expand All @@ -247,10 +255,10 @@ public static void writeToTestSuiteJson(Map<String, TestSuite> testSuiteMap, Pat
String json = gson.toJson(testSuiteMap);
writer.write(new String(json.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e);
}
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.cli.cmd;

import io.ballerina.cli.launcher.BLauncherException;
import io.ballerina.cli.utils.TestUtils;
import io.ballerina.projects.ProjectEnvironmentBuilder;
import io.ballerina.projects.environment.Environment;
import io.ballerina.projects.environment.EnvironmentBuilder;
Expand All @@ -27,6 +28,8 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.ballerinalang.test.BCompileUtil;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -223,7 +226,7 @@ public void testTestCommandPreservingBinJarInTargetDir() throws IOException {
.resolve("foo-winery-0.1.0-testable.jar").toFile().exists());
}

@Test
@Test(description = "Test a ballerina project with an invalid argument for --coverage-format")
public void testUnsupportedCoverageFormat() throws IOException {
Path projectPath = this.testResources.resolve("validProjectWithTests");
TestCommand testCommand = new TestCommand(
Expand All @@ -236,7 +239,7 @@ public void testUnsupportedCoverageFormat() throws IOException {
"supported."));
}

@Test ()
@Test (description = "Test a ballerina project with a custom value for --target-dir")
public void testCustomTargetDirWithTestCmd() {
Path projectPath = this.testResources.resolve("validProjectWithTests");
Path customTargetDir = projectPath.resolve("customTargetDir3");
Expand All @@ -260,6 +263,31 @@ public void testCustomTargetDirWithTestCmd() {
".json")));
}

@Test(description = "Test a ballerina project with --test-report")
public void testTestWithReport() {
Path projectPath = this.testResources.resolve("validProjectWithTests");
TestCommand testCommand = new TestCommand(
projectPath, printStream, printStream, false, true, false, null);
new CommandLine(testCommand).parseArgs();
try (MockedStatic<TestUtils> testUtilsMockedStatic = Mockito.mockStatic(
TestUtils.class, Mockito.CALLS_REAL_METHODS)) {
testUtilsMockedStatic.when(TestUtils::getReportToolsPath)
.thenReturn(projectPath.resolve("resources").resolve("coverage").resolve("report.zip"));
testCommand.execute();
}
Path reportDir = projectPath.resolve("target").resolve("report");

Assert.assertTrue(Files.exists(reportDir));
Assert.assertTrue(Files.exists(reportDir.resolve("favicon.ico")));
Assert.assertTrue(Files.exists(reportDir.resolve("index.html")));
Assert.assertTrue(Files.exists(reportDir.resolve("test_results.json")));
Assert.assertTrue(Files.exists(reportDir.resolve("manifest.json")));
Assert.assertTrue(Files.exists(reportDir.resolve("static").resolve("css").resolve("2.d5162072.chunk.css")));
Assert.assertTrue(Files.exists(reportDir.resolve("static").resolve("css").resolve("main.15691da7.chunk.css")));
Assert.assertTrue(Files.exists(reportDir.resolve("static").resolve("js").resolve("2.bc541f30.chunk.js")));
Assert.assertTrue(Files.exists(reportDir.resolve("static").resolve("js").resolve("main.ea323a3b.chunk.js")));
}

@Test(description = "tests bal test command with sticky flag")
public void testBalTestWithStickyFlag() throws IOException {
// Cache package pkg_a 1.0.0
Expand Down Expand Up @@ -408,5 +436,4 @@ public void testGraalVMIncompatibleProject() throws IOException {
Assert.assertTrue(buildLog.contains("WARNING: Package is not compatible with GraalVM."));
}
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.ballerinalang.compiler.bir;

import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
Expand Down Expand Up @@ -363,11 +364,11 @@ public void visit(BLangTypeDefinition astTypeDefinition) {
BType type = getDefinedType(astTypeDefinition);
BType referredType = Types.getImpliedType(type);
BSymbol symbol = astTypeDefinition.symbol;
Name displayName = symbol.name;
String displayName = symbol.name.value;
if (referredType.tag == TypeTags.RECORD) {
BRecordType recordType = (BRecordType) referredType;
if (recordType.shouldPrintShape()) {
displayName = new Name(recordType.toString());
displayName = recordType.toString();
}
}

Expand All @@ -378,7 +379,7 @@ public void visit(BLangTypeDefinition astTypeDefinition) {
type,
new ArrayList<>(),
symbol.origin.toBIROrigin(),
displayName,
new Name(Utils.unescapeBallerina(displayName)),
symbol.originalName);
if (symbol.tag == SymTag.TYPE_DEF) {
BTypeReferenceType referenceType = ((BTypeDefinitionSymbol) symbol).referenceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableTypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BObjectTypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols;
import org.wso2.ballerinalang.compiler.semantics.model.types.BField;
import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType;
Expand All @@ -43,7 +42,6 @@
import org.wso2.ballerinalang.compiler.util.Name;
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeTags;
import org.wso2.ballerinalang.util.Flags;
import org.wso2.ballerinalang.util.Lists;

import java.util.ArrayList;
Expand Down Expand Up @@ -192,9 +190,6 @@ private static void encodePackageIdentifiers(PackageID packageID, HashMap<String
private static void encodeTypeDefIdentifiers(List<BIRTypeDefinition> typeDefs,
HashMap<String, String> encodedVsInitialIds) {
for (BIRTypeDefinition typeDefinition : typeDefs) {
if (Symbols.isFlagOn(typeDefinition.type.flags, Flags.ANONYMOUS)) {
typeDefinition.name = Names.fromString(Utils.unescapeBallerina(typeDefinition.name.value));
}
typeDefinition.type.tsymbol.name = Names.fromString(encodeNonFunctionIdentifier(
typeDefinition.type.tsymbol.name.value, encodedVsInitialIds));
typeDefinition.internalName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,12 @@ private STNode parseXMLText() {
* @return XML char-data token
*/
private STNode parseCharData() {
return consume();
STToken token = consume();
if (token.kind != SyntaxKind.XML_TEXT_CONTENT) {
return STNodeFactory.createLiteralValueToken(SyntaxKind.XML_TEXT_CONTENT, token.text(),
token.leadingMinutiae(), token.trailingMinutiae(), token.diagnostics());
}
return token;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,8 @@ public void testMissingNameInXMLEndTag() {
public void testMissingQuoteInXMLAttributeValue() {
testFile("xml-template/xml_template_source_35.bal", "xml-template/xml_template_assert_35.json");
}

public void testMissingClosingAndNextStartingAngleBracket() {
testFile("xml-template/xml_template_source_36.bal", "xml-template/xml_template_assert_36.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
],
"trailingMinutiae": [
Expand Down Expand Up @@ -203,7 +203,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public function main() {
foo(object {
int i = 1;
});
int i = 1;
});
}
Loading

0 comments on commit 16c6d1a

Please sign in to comment.