Skip to content

Commit

Permalink
Merge pull request #145 from codacy/bump-things-up
Browse files Browse the repository at this point in the history
Updates to latest versions (IO-397)
  • Loading branch information
vascorsd authored Feb 9, 2023
2 parents 01a3c36 + 7134763 commit 033dc07
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 111 deletions.
15 changes: 7 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
codacy: codacy/[email protected].0
codacy: codacy/[email protected].1
codacy_plugins_test: codacy/[email protected]

workflows:
Expand All @@ -10,9 +10,9 @@ workflows:
jobs:
- codacy/checkout_and_version
- codacy/shell:
name: build_test_publish_local
name: publish_local
cmd: |
docker build . -t $CIRCLE_PROJECT_REPONAME:latest
docker build -t $CIRCLE_PROJECT_REPONAME:latest .
docker save --output docker-image.tar $CIRCLE_PROJECT_REPONAME:latest
persist_to_workspace: true
requires:
Expand All @@ -23,15 +23,14 @@ workflows:
run_json_tests: false
run_pattern_tests: false
requires:
- build_test_publish_local
- publish_local
- codacy/publish_docker:
context: CodacyDocker
requires:
- plugins_test
filters:
branches:
only:
- master
only: master
requires:
- plugins_test
- codacy/tag_version:
name: tag_version
context: CodacyAWS
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
</ItemGroup>
</Project>


57 changes: 20 additions & 37 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
FROM ubuntu:20.04 as base
ARG DOTNET_VERSION=6.0
ARG DOTNET_BASE_OS=alpine3.17

# setup mono repo
RUN apt -y update && \
apt install -y --no-install-recommends gnupg ca-certificates && \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF && \
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | tee /etc/apt/sources.list.d/mono-official-stable.list && \
apt purge -y --auto-remove gnupg && \
apt -y update


FROM base as builder

ENV TZ=Europe/Lisbon
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install complete mono and its dependencies
RUN apt -y install mono-complete build-essential unzip

# Install dotnet sdk
RUN apt-get -y update && \
apt-get install -y apt-transport-https wget make && \
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
dpkg -i packages-microsoft-prod.deb && \
rm packages-microsoft-prod.deb && \
apt-get update && \
apt-get install -y dotnet-sdk-6.0
## BUILD IMAGE
FROM mcr.microsoft.com/dotnet/sdk:$DOTNET_VERSION-$DOTNET_BASE_OS AS builder

COPY . /workdir
WORKDIR /workdir

RUN make publish
RUN make run-tests
RUN apk add --no-cache make &&\
make &&\
make publish


## RUNTIME IMAGE
FROM mcr.microsoft.com/dotnet/runtime:$DOTNET_VERSION-$DOTNET_BASE_OS

COPY --from=builder /workdir/src/Analyzer/bin/Release/net6/publish/ /opt/docker/bin/

FROM alpine:3.17
# Create NON-ROOT user
RUN adduser -u 2004 -D docker

COPY --from=builder /workdir/src/Analyzer/bin/Release/net48/publish/Analyzer.exe /opt/docker/bin/
COPY --from=builder /workdir/src/Analyzer/bin/Release/net48/publish/*.dll /opt/docker/bin/
# From now on, run as NON-ROOT user
USER docker

RUN apk add --no-cache mono --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing &&\
apk add --no-cache --virtual=.build-dependencies ca-certificates &&\
cert-sync /etc/ssl/certs/ca-certificates.crt &&\
apk del .build-dependencies &&\
adduser -u 2004 -D docker
# Disable diagnostics stuff from dotnet that are turned on by default.
# Should make the image even more "read-only".
ENV DOTNET_EnableDiagnostics=0

ENTRYPOINT [ "mono", "/opt/docker/bin/Analyzer.exe" ]
ENTRYPOINT [ "dotnet", "/opt/docker/bin/Analyzer.dll" ]
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
export FrameworkPathOverride=$(shell dirname $(shell which mono))/../lib/mono/4.5/
BUILD_CMD=dotnet build --no-restore /property:GenerateFullPaths=true

LIBRARIES_FOLDER=.lib
PACKAGES_FOLDER=.packages
RESOURCE_FOLDER=.res

all: configure build

configure:
Expand All @@ -22,11 +17,13 @@ build-seed:
$(BUILD_CMD) src/Seed

publish:
dotnet restore
dotnet publish -c Release -f net48
dotnet publish -c Release -f net6

run:
dotnet run --project src/Analyzer -f net6

run-tests:
dotnet test

clean:
rm -rf .lib/ .packages/ .res/
rm -rf packages
4 changes: 2 additions & 2 deletions src/Analyzer/Analyzer.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net48</TargetFrameworks>
<TargetFrameworks>net6</TargetFrameworks>
<RootNamespace>CodacyCSharp.Metrics.Analyzer</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.4" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.30.0.37606" GeneratePathProperty="true" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.52.0.60960" GeneratePathProperty="true" />
</ItemGroup>
<ItemGroup>
<Reference Include="SonarAnalyzer.CSharp">
Expand Down
47 changes: 36 additions & 11 deletions src/Analyzer/CodeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -46,21 +47,45 @@ public async Task<CodacyMetricsResult> Analyze(string file, CancellationToken ca
result.NrMethods = metrics.FunctionCount;
result.NrClasses = metrics.ClassCount;

result.LineComplexities = metrics.FunctionNodes.GroupBy(row =>
row.GetLocation().GetMappedLineSpan().Span.Start.Line + 1)
.Select(nodeGroup =>
var functionMetrics = new CodacyCSharpFunctionMetrics(syntaxTree, semanticModel);

// total complexity of file is the biggest complexity of the functions in the file.
result.LineComplexities = functionMetrics.functionComplexities;
result.Complexity = functionMetrics.functionComplexities
.Select(functionComplexity => functionComplexity.Value)
.DefaultIfEmpty(0)
.Max();

return result;
}
}

// workaround to get stuff only available as protected.
public class CodacyCSharpFunctionMetrics : CSharpMetrics
{
public IEnumerable<LineComplexity> functionComplexities { get; }

public CodacyCSharpFunctionMetrics(
Microsoft.CodeAnalysis.SyntaxTree tree,
Microsoft.CodeAnalysis.SemanticModel semanticModel) : base(tree, semanticModel)
{
// each function found in the file and the cyclomatic complexity of each of those functions
this.functionComplexities = tree.GetRoot()
.DescendantNodes()
.Where(IsFunction)
.Select(functionNode =>
(functionNode.GetLocation().GetMappedLineSpan().Span.Start.Line + 1, functionNode)
)
.Select(lineAndFunctionNode =>
{
var lineComplexity = nodeGroup.Max(num => metrics.GetCyclomaticComplexity(num));
result.Complexity = Math.Max(result.Complexity, lineComplexity);
(var line, var node) = lineAndFunctionNode;

return new LineComplexity
{
Line = nodeGroup.Key,
Value = lineComplexity
Line = line,
Value = this.ComputeCyclomaticComplexity(node)
};
})
.ToList();

return result;
});
}
}
}
2 changes: 1 addition & 1 deletion src/Seed/Seed.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net48</TargetFrameworks>
<TargetFrameworks>net6</TargetFrameworks>
<RootNamespace>CodacyCSharp.Metrics.Seed</RootNamespace>
</PropertyGroup>
<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions test/Analyzer.Tests/Analyzer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<TargetFrameworks>net48</TargetFrameworks>
<TargetFrameworks>net6</TargetFrameworks>
<RootNamespace>CodacyCSharp.Metrics.Analyzer.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.4.1" Condition="$(TargetFramework.StartsWith('net4')) AND '$(OS)' == 'Unix'" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Analyzer\Analyzer.csproj" />
Expand Down
87 changes: 45 additions & 42 deletions test/Analyzer.Tests/CodeAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -6,46 +7,48 @@

namespace CodacyCSharp.Metrics.Analyzer.Tests
{
public class CodeAnalyzerTests
{
private const string sourceFile = "Astar.cs";

[Fact]
public async Task ComplexityMathTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

Assert.True(result.LineComplexities.ToArray().Length == result.NrMethods,
"Line complexity doesn't match");
Assert.True(result.Complexity == result.LineComplexities.Max(x => x.Value),
"Complexity is not calculated correctly");
}

[Fact]
public async Task ResultCheckTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

// check if every field is correct
Assert.Equal(sourceFile, result.Filename);
Assert.Equal(131, result.Loc);
Assert.Equal(4, result.Cloc);
Assert.Equal(8, result.NrMethods);
Assert.Equal(2, result.NrClasses);
Assert.Equal(10, result.Complexity);
}

[Fact]
public async Task ResultJsonCheckTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

// check json parsing
var expectedResult = File.ReadLines("Resources/Astar.json").First();
Assert.True(result.ToString() == expectedResult, "Wrong json format");
}
}
public class CodeAnalyzerTests
{
private const string sourceFile = "Astar.cs";

[Fact]
public async Task ComplexityMathTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

Assert.True(result.LineComplexities.ToArray().Length == result.NrMethods,
"Line complexity doesn't match");

Assert.True(result.Complexity == result.LineComplexities.Max(x => x.Value),
"Complexity is not calculated correctly");
}

[Fact]
public async Task ResultCheckTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

// check if every field is correct
Assert.Equal(sourceFile, result.Filename);
Assert.Equal(131, result.Loc);
Assert.Equal(4, result.Cloc);
Assert.Equal(8, result.NrMethods);
Assert.Equal(2, result.NrClasses);
Assert.Equal(10, result.Complexity);
}

[Fact]
public async Task ResultJsonCheckTest()
{
var codeAnalyzer = new CodeAnalyzer();
var result = await codeAnalyzer.Analyze(sourceFile, CancellationToken.None);

// check json parsing
var expectedResult = File.ReadLines("Resources/Astar.json").First();

Assert.True(result.ToString() == expectedResult, "Wrong json format");
}
}
}

0 comments on commit 033dc07

Please sign in to comment.