Skip to content

Commit

Permalink
Add language core model, adapt conversions and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Jan 22, 2025
1 parent 9a1b6f4 commit 77e2103
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/Core.UnitTests/LanguageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public void Language_Ctor_ArgChecks()
act = () => new Language(null, key, fileSuffix, serverLanguage);
act.Should().ThrowExactly<ArgumentNullException>().And.ParamName.Should().Be("id");

act = () => new Language(name, key, null, serverLanguage);
act.Should().ThrowExactly<ArgumentNullException>().And.ParamName.Should().Be("fileSuffix");

act = () => new Language(name, key, fileSuffix, null);
act.Should().ThrowExactly<ArgumentNullException>().And.ParamName.Should().Be("serverLanguage");
}
Expand Down
19 changes: 8 additions & 11 deletions src/Core/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ public sealed class Language : IEquatable<Language>
public readonly static Language Unknown = new Language();
public readonly static Language CSharp = new Language("CSharp", CoreStrings.CSharpLanguageName, "sonarlint_csharp.globalconfig", SonarQubeLanguage.CSharp);
public readonly static Language VBNET = new Language("VB", CoreStrings.VBNetLanguageName, "sonarlint_vb.globalconfig", SonarQubeLanguage.VbNet);
public readonly static Language Cpp = new Language("C++", CoreStrings.CppLanguageName, "cpp_settings.json", SonarQubeLanguage.Cpp);
public readonly static Language C = new Language("C", "C", "c_settings.json", SonarQubeLanguage.C);
public readonly static Language Js = new Language("Js", "JavaScript", "js_settings.json", SonarQubeLanguage.Js);
public readonly static Language Ts = new Language("Ts", "TypeScript", "ts_settings.json", SonarQubeLanguage.Ts);
public readonly static Language Css = new Language("Css", "CSS", "css_settings.json", SonarQubeLanguage.Css);
public readonly static Language Secrets = new Language("Secrets", "Secrets", "secrets_settings.json", SonarQubeLanguage.Secrets);
public readonly static Language Cpp = new Language("C++", CoreStrings.CppLanguageName, null, SonarQubeLanguage.Cpp);
public readonly static Language C = new Language("C", "C", null, SonarQubeLanguage.C);
public readonly static Language Js = new Language("Js", "JavaScript", null, SonarQubeLanguage.Js);
public readonly static Language Ts = new Language("Ts", "TypeScript", null, SonarQubeLanguage.Ts);
public readonly static Language Css = new Language("Css", "CSS", null, SonarQubeLanguage.Css);
public readonly static Language Html = new Language("Html", "HTML", null, SonarQubeLanguage.Html);
public readonly static Language Secrets = new Language("Secrets", "Secrets", null, SonarQubeLanguage.Secrets);

/// <summary>
/// Returns the language for the specified language key, or null if it does not match a known language
Expand Down Expand Up @@ -136,6 +137,7 @@ public static IEnumerable<Language> KnownLanguages
{ "typescript", Ts },
{ "tssecurity", Ts },
{ "css", Css },
{ "Web", Html},
{ "secrets", Secrets}
};

Expand All @@ -161,11 +163,6 @@ public Language(string id, string name, string fileSuffix, SonarQubeLanguage ser
throw new ArgumentNullException(nameof(name));
}

if (string.IsNullOrWhiteSpace(fileSuffix))
{
throw new ArgumentNullException(nameof(fileSuffix));
}

this.Id = id;
this.Name = name;
this.FileSuffixAndExtension = fileSuffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void StandaloneLanguages_ShouldBeExpected()
Language.JS,
Language.TS,
Language.CSS,
Language.HTML,
Language.C,
Language.CPP,
Language.CS,
Expand All @@ -149,6 +150,7 @@ public void AnalyzableLanguages_ShouldBeExpected()
Language.JS,
Language.TS,
Language.CSS,
Language.HTML,
Language.C,
Language.CPP,
Language.SECRETS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,21 @@ public void ListConnectedModeEmbeddedPluginPathsByKey_JarsExists_ContainsEntryFo
MockFileSystem(true,
BuildJarFullPath("sonar-text-plugin-2.15.0.3845.jar"),
BuildJarFullPath("sonar-javascript-plugin-10.14.0.26080.jar"),
BuildJarFullPath("sonar-html-plugin-3.18.0.5605.jar"),
BuildJarFullPath("sonar-cfamily-plugin-6.58.0.74356.jar"));

var result = testSubject.ListConnectedModeEmbeddedPluginPathsByKey();

result.Count.Should().Be(2);
result.Count.Should().Be(3);
VerifyContainsPlugin(result, "text", "sonar-text-plugin-2.15.0.3845.jar");
VerifyContainsPlugin(result, "web", "sonar-html-plugin-3.18.0.5605.jar");
VerifyContainsPlugin(result, "javascript", "sonar-javascript-plugin-10.14.0.26080.jar");
}

[TestMethod]
[DataRow("text", "sonar-text-plugin-2.15.0.3845.jar", "sonar-text-plugin-2.16.0.4008.jar")]
[DataRow("javascript", "sonar-javascript-plugin-10.14.0.26080.jar", "sonar-javascript-plugin-10.15.0.0080.jar")]
[DataRow("web", "sonar-html-plugin-10.14.0.26080.jar", "sonar-html-plugin-10.15.0.0080.jar")]
public void ListConnectedModeEmbeddedPluginPathsByKey_MultiplePluginVersionsExist_ReturnsTheFirstOneAndLogs(string pluginKey, string version1, string version2)
{
MockFileSystem(true, BuildJarFullPath(version1), BuildJarFullPath(version2));
Expand All @@ -129,6 +132,7 @@ public void ListConnectedModeEmbeddedPluginPathsByKey_MultiplePluginVersionsExis
[TestMethod]
[DataRow("text", "sonar-text-plugin-2.15.0.3845.jar", "sonar-text-plugin-enterprise-2.15.0.3845.jar")]
[DataRow("javascript", "sonar-javascript-plugin-10.14.0.26080.jar", "sonar-javascript-plugin-enterprise-10.14.0.26080.jar")]
[DataRow("web", "sonar-html-plugin-10.14.0.26080.jar", "sonar-html-plugin-enterprise-10.14.0.26080.jar")]
public void ListConnectedModeEmbeddedPluginPathsByKey_PluginsWithDifferentNameExists_ReturnsCorrectOne(string pluginKey, string correct, string wrong)
{
MockFileSystem(true, BuildJarFullPath(wrong), BuildJarFullPath(correct));
Expand All @@ -146,7 +150,7 @@ public void ListConnectedModeEmbeddedPluginPathsByKey_NoJars_ReturnsEmptyDiction
var result = testSubject.ListConnectedModeEmbeddedPluginPathsByKey();

result.Should().BeEmpty();
logger.Received(2).LogVerbose(Strings.ConnectedModeEmbeddedPluginJarLocator_JarsNotFound);
logger.Received(3).LogVerbose(Strings.ConnectedModeEmbeddedPluginJarLocator_JarsNotFound);
}

private void MockFileSystem(bool directoryExists, params string[] files)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class SLCoreEmbeddedPluginJarLocator : ISLCoreEmbeddedPluginJarLocator
private readonly Dictionary<string, string> connectedModePluginNamePatternByPluginKey = new()
{
{ "text", "sonar-text-plugin-(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.jar" },
{ "javascript", "sonar-javascript-plugin-(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.jar" }
{ "javascript", "sonar-javascript-plugin-(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.jar" },
{ "web", "sonar-html-plugin-(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.jar"}
};
private readonly IVsixRootLocator vsixRootLocator;
private readonly IFileSystem fileSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public async Task CheckAllEmbeddedRules()
"csharpsquid:S6932", // unsupported <dl> and <dt> tag https://github.com/SonarSource/sonarlint-visualstudio/issues/5414
"csharpsquid:S6966", // unsupported <dl> and <dt> tag https://github.com/SonarSource/sonarlint-visualstudio/issues/5414
"typescript:S6811", // unsupported <caption> tag https://github.com/SonarSource/sonarlint-visualstudio/issues/5014
"javascript:S6811" // unsupported <caption> tag https://github.com/SonarSource/sonarlint-visualstudio/issues/5014
"javascript:S6811", // unsupported <caption> tag https://github.com/SonarSource/sonarlint-visualstudio/issues/5014
"Web:S6811" //
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/SLCore/Common/Helpers/LanguageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static VisualStudio.Core.Language ConvertToCoreLanguage(this Language lan
Language.CPP => VisualStudio.Core.Language.Cpp,
Language.CS => VisualStudio.Core.Language.CSharp,
Language.CSS => VisualStudio.Core.Language.Css,
Language.HTML => VisualStudio.Core.Language.Html,
Language.JS => VisualStudio.Core.Language.Js,
Language.SECRETS => VisualStudio.Core.Language.Secrets,
Language.TS => VisualStudio.Core.Language.Ts,
Expand All @@ -46,6 +47,7 @@ public static string GetPluginKey(this Language language) =>
Language.CS => "csharpenterprise",
Language.VBNET => "vbnetenterprise",
Language.SECRETS => "text",
Language.HTML => "web",
_ => null
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task InvokeAsync_ReturnsCorrectStream()

MocksHelper.SetupHttpRequest(
messageHandler,
requestRelativePath: "api/push/sonarlint_events?languages=cs%2Cvbnet%2Ccpp%2Cc%2Cjs%2Cts%2Csecrets%2Ccss&projectKeys=someproj",
requestRelativePath: "api/push/sonarlint_events?languages=cs%2Cvbnet%2Ccpp%2Cc%2Cjs%2Cts%2Csecrets%2Ccss%2Cweb&projectKeys=someproj",
responseMessage: new HttpResponseMessage { Content = new StreamContent(testedStream) },
headers: MediaTypeHeaderValue.Parse("text/event-stream"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task CreateSSEStreamReader_SupportedServerVersion_CreatesSession()

await ConnectToSonarQube("9.4.0.0");

SetupRequest("api/push/sonarlint_events?languages=cs%2Cvbnet%2Ccpp%2Cc%2Cjs%2Cts%2Csecrets%2Ccss&projectKeys=myProject",
SetupRequest("api/push/sonarlint_events?languages=cs%2Cvbnet%2Ccpp%2Cc%2Cjs%2Cts%2Csecrets%2Ccss%2Cweb&projectKeys=myProject",
new HttpResponseMessage
{
Content = new StreamContent(Stream.Null)
Expand Down
3 changes: 2 additions & 1 deletion src/SonarQube.Client/Models/SonarQubeLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public class SonarQubeLanguage
public static readonly SonarQubeLanguage Js = new SonarQubeLanguage("js", "JavaScript", "javascript");
public static readonly SonarQubeLanguage Ts = new SonarQubeLanguage("ts", "TypeScript", "typescript");
public static readonly SonarQubeLanguage Css = new SonarQubeLanguage("css", "CSS", "css");
public static readonly SonarQubeLanguage Html = new SonarQubeLanguage("web", "HTML", "html");
public static readonly SonarQubeLanguage Secrets = new SonarQubeLanguage("secrets", "Secrets", "SonarSecrets");

public static readonly SonarQubeLanguage[] AllLanguages =
{
CSharp, VbNet, Cpp, C, Js, Ts, Secrets, Css
CSharp, VbNet, Cpp, C, Js, Ts, Secrets, Css, Html
};

public string Key { get; }
Expand Down

0 comments on commit 77e2103

Please sign in to comment.