diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..808f9eb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +end_of_line = crlf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.{cs,csproj,props,targets,sln}] +charset = utf-8-bom + +[*.{json,yml,yaml}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..aac1598 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,23 @@ +version: 2 +updates: + - package-ecosystem: nuget + directory: / + schedule: + interval: weekly + open-pull-requests-limit: 10 + commit-message: + prefix: chore + include: scope + groups: + tests: + patterns: ["xunit*", "AwesomeAssertions"] + ignore: + # Never reintroduce FluentAssertions (commercial license since v8) + - dependency-name: "FluentAssertions" + + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + commit-message: + prefix: ci diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 1bc1638..c1e4f22 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -1,15 +1,15 @@ name: NetDevPack Brasil - MASTER Deploy + on: push: branches: [ master ] workflow_dispatch: inputs: version: - description: Existing release to publish again (e.g. 2.0.0). Builds tag v and pushes it to nuget.org. + description: Existing release to publish again (e.g. 3.0.0). Builds tag v and pushes it to nuget.org. required: true env: - REPOSITORY_URL: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json CURRENT_REPO_URL: https://github.com/${{ github.repository }} permissions: @@ -23,66 +23,59 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }} - - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x - - - name: Setup .NET 9 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 9.0.x + - name: Checkout repository + uses: actions/checkout@v7 + with: + fetch-depth: 0 + ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }} - - name: Setup .NET 10 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 10.0.x + - name: Setup .NET + uses: actions/setup-dotnet@v6 + with: + global-json-file: global.json - - name: Restore dependencies - run: dotnet restore + - name: Restore dependencies + run: dotnet restore - - name: Build - run: dotnet build --no-restore + - name: Build + run: dotnet build --no-restore -c Release - - name: Test - run: dotnet test + - name: Test + run: dotnet test --no-build -c Release - - name: Semantic Release - id: semantic - if: github.event_name == 'push' - uses: cycjimmy/semantic-release-action@v4 - with: - extra_plugins: | - @semantic-release/changelog - @semantic-release/git - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Semantic Release + id: semantic + if: github.event_name == 'push' + uses: cycjimmy/semantic-release-action@v4 + with: + semantic_version: 24 + extra_plugins: | + @semantic-release/changelog@6 + @semantic-release/git@10 + conventional-changelog-conventionalcommits@8 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Resolve version - id: version - if: github.event_name == 'workflow_dispatch' || steps.semantic.outputs.new_release_published == 'true' - env: - VERSION: ${{ inputs.version || steps.semantic.outputs.new_release_version }} - run: | - [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || { echo "::error::Invalid version '$VERSION'"; exit 1; } - echo "value=$VERSION" >> "$GITHUB_OUTPUT" + - name: Resolve version + id: version + if: github.event_name == 'workflow_dispatch' || steps.semantic.outputs.new_release_published == 'true' + env: + VERSION: ${{ inputs.version || steps.semantic.outputs.new_release_version }} + run: | + [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || { echo "::error::Invalid version '$VERSION'"; exit 1; } + echo "value=$VERSION" >> "$GITHUB_OUTPUT" - - name: Generate Package - if: steps.version.outputs.value != '' - run: dotnet pack -c Release -o out -p:PackageVersion=${{ steps.version.outputs.value }} -p:RepositoryUrl=${{env.CURRENT_REPO_URL}} + - name: Generate Package + if: steps.version.outputs.value != '' + run: dotnet pack src/NetDevPack.Brasil/NetDevPack.Brasil.csproj -c Release -o out -p:PackageVersion=${{ steps.version.outputs.value }} -p:RepositoryUrl=${{ env.CURRENT_REPO_URL }} - - name: NuGet login (trusted publishing) - id: nuget - if: steps.version.outputs.value != '' - uses: NuGet/login@v1 - with: - user: ${{ vars.NUGET_USER }} + - name: NuGet login (trusted publishing) + id: nuget + if: steps.version.outputs.value != '' + uses: NuGet/login@v1 + with: + user: ${{ vars.NUGET_USER }} - - name: Publish the package to nuget.org - if: steps.version.outputs.value != '' - run: dotnet nuget push ./out/*.nupkg --skip-duplicate -n -d -k ${{ steps.nuget.outputs.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json + - name: Publish the package to nuget.org + if: steps.version.outputs.value != '' + run: dotnet nuget push ./out/*.nupkg --skip-duplicate -k ${{ steps.nuget.outputs.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 68690c1..b5e98aa 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,36 +1,37 @@ name: NetDevPack Brasil - MASTER PR + on: pull_request: branches: [ master ] +permissions: + contents: read + jobs: pull-request: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x + - name: Checkout repository + uses: actions/checkout@v7 + with: + fetch-depth: 0 - - name: Setup .NET 9 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 9.0.x + - name: Setup .NET + uses: actions/setup-dotnet@v6 + with: + global-json-file: global.json - - name: Setup .NET 10 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 10.0.x + - name: Restore dependencies + run: dotnet restore - - name: Restore dependencies - run: dotnet restore + - name: Build + run: dotnet build --no-restore -c Release - - name: Build - run: dotnet build --no-restore + - name: Test + run: dotnet test --no-build -c Release - - name: Test - run: dotnet test --no-build --verbosity normal + - name: Check vulnerable packages + run: | + dotnet list package --vulnerable --include-transitive 2>&1 | tee vulnerable.txt + ! grep -q "has the following vulnerable packages" vulnerable.txt diff --git a/.releaserc b/.releaserc index ce9cb40..2ffccd3 100644 --- a/.releaserc +++ b/.releaserc @@ -1,11 +1,22 @@ { - "branches": ["master"], - - "plugins": [ - "@semantic-release/commit-analyzer", - "@semantic-release/release-notes-generator", - "@semantic-release/changelog", - "@semantic-release/github", - "@semantic-release/git" - ] -} \ No newline at end of file + "branches": [ + "master" + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "conventionalcommits" + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits" + } + ], + "@semantic-release/changelog", + "@semantic-release/github", + "@semantic-release/git" + ] +} diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..a5c8809 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,11 @@ + + + + latest + + true + all + $(WarningsAsErrors);NU1901;NU1902;NU1903;NU1904 + true + + diff --git a/Directory.Packages.props b/Directory.Packages.props new file mode 100644 index 0000000..5d2f2ac --- /dev/null +++ b/Directory.Packages.props @@ -0,0 +1,17 @@ + + + + true + true + + + + + + + + + + diff --git a/README.md b/README.md index 6137d8c..9b725ff 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,52 @@ - .NET DevPack -What is the .NET DevPack? +O que é o .NET DevPack Brasil? ===================== -A smart set of common classes and implementations to improve your development productivity. +Uma coleção de utilidades para o Brasil: documentos nacionais (CPF, CNPJ, Título de Eleitor) e CEP, com validação e formatação. Foi feito para ser usado junto com o [NetDevPack](https://github.com/NetDevPack/NetDevPack). -## Give a Star! :star: -If you liked the project or if NetDevPack helped you, please give a star ;) +## Dê uma estrela! :star: +Se você gostou do projeto ou se o NetDevPack te ajudou, deixe uma estrela ;) -## Get Started -.NET DevPack can be installed using the Nuget package manager or the `dotnet` CLI. +## Como começar ``` -dotnet add package NetDevPack +dotnet add package NetDevPack.Brasil ``` -For ASP.NET Core Identity Pack: -``` -dotnet add package NetDevPack.Identity -``` +| Pacote | Versão | Downloads | +| ------ | ------ | --------- | +| `NetDevPack.Brasil` | [![NuGet](https://img.shields.io/nuget/v/NetDevPack.Brasil.svg)](https://nuget.org/packages/NetDevPack.Brasil) | [![Nuget](https://img.shields.io/nuget/dt/NetDevPack.Brasil.svg)](https://nuget.org/packages/NetDevPack.Brasil) | + +### Compatibilidade ---- +| Versão | Target framework | NetDevPack | +| ------ | ---------------- | ---------- | +| 3.x | .NET 10 (LTS) | 9.x | +| 2.x | .NET Standard 2.1, .NET 8, 9 e 10 | 8.x | -| Package | Version | Popularity | -| ------- | ----- | ----- | -| `NetDevPack` | [![NuGet](https://img.shields.io/nuget/v/NetDevPack.svg)](https://nuget.org/packages/NetDevPack) | [![Nuget](https://img.shields.io/nuget/dt/NetDevPack.svg)](https://nuget.org/packages/NetDevPack) | -| `NetDevPack.Identity` | [![NuGet](https://img.shields.io/nuget/v/NetDevPack.Identity.svg)](https://nuget.org/packages/NetDevPack.Identity) | [![Nuget](https://img.shields.io/nuget/dt/NetDevPack.Identity.svg)](https://nuget.org/packages/NetDevPack.Identity) -| `NetDevPackBr` | [![NuGet](https://img.shields.io/nuget/v/NetDevPackBr.svg)](https://nuget.org/packages/NetDevPackBr) | [![Nuget](https://img.shields.io/nuget/dt/NetDevPackBr.svg)](https://nuget.org/packages/NetDevPackBr) +## Exemplos +Os construtores validam o valor e lançam `NetDevPack.Domain.DomainException` quando ele é inválido. Máscara e caracteres não numéricos são ignorados. -### Examples -- Coming soon. +```csharp +using NetDevPack.Brasil.Documentos; +using NetDevPack.Brasil.Localizacao; -### Documentation +var cpf = new Cpf("915.212.540-87"); +cpf.SemMascara(); // "91521254087" +cpf.ComMascara(); // "915.212.540-87" -- Coming soon. +var cnpj = new Cnpj("11.444.777/0001-61"); +cnpj.ComMascara(); // "11.444.777/0001-61" + +var titulo = new TituloEleitor("1023 8501 0671"); + +var cep = new Cep("01310-100"); +cep.EstaValido(); // true +cep.ComMascara(); // "01310-100" +``` +Também dá para validar sem criar o objeto, com `CpfValidador`, `CnpjValidador` e `TituloEleitorValidator` (namespace `NetDevPack.Brasil.Documentos.Validacao`) e `CepValidador` (`NetDevPack.Brasil.Localizacao.Validacao`). -## About -.NET DevPack was developed by [Eduardo Pires](http://eduardopires.net.br) under the [MIT license](LICENSE). +## Sobre +O .NET DevPack Brasil foi desenvolvido por [Eduardo Pires](http://eduardopires.net.br) sob a [licença MIT](LICENSE). diff --git a/global.json b/global.json new file mode 100644 index 0000000..1d364c6 --- /dev/null +++ b/global.json @@ -0,0 +1,9 @@ +{ + "sdk": { + "version": "10.0.100", + "rollForward": "latestFeature" + }, + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/src/NetDevPack.Brasil/Localizacao/Cep.cs b/src/NetDevPack.Brasil/Localizacao/Cep.cs index 24c2164..c371b96 100644 --- a/src/NetDevPack.Brasil/Localizacao/Cep.cs +++ b/src/NetDevPack.Brasil/Localizacao/Cep.cs @@ -1,5 +1,6 @@ using NetDevPack.Brasil.Localizacao.Validacao; using NetDevPack.Domain; +using NetDevPack.Utilities; using System; namespace NetDevPack.Brasil.Localizacao @@ -62,8 +63,9 @@ public class Cep : IEquatable /// CEP (Código de Endereçamento Postal) inválido. public Cep(string codigo) { - Codigo = codigo; _validador = new CepValidador(codigo); + // Same as Cpf/Cnpj: keep only the digits, so the mask doesn't leak into SemMascara/ComMascara/Equals + Codigo = EstaValido() ? codigo.OnlyNumbers() : codigo; if (EstaValido()) PreencherEstrutura(); } diff --git a/src/NetDevPack.Brasil/NetDevPack.Brasil.csproj b/src/NetDevPack.Brasil/NetDevPack.Brasil.csproj index 90afc2c..c02e2ed 100644 --- a/src/NetDevPack.Brasil/NetDevPack.Brasil.csproj +++ b/src/NetDevPack.Brasil/NetDevPack.Brasil.csproj @@ -1,24 +1,24 @@ - + - netstandard2.1;net8.0;net9.0;net10.0 + net10.0 false - Eduardo Pires + EduardoPires desenvolvedor.io .NET DevPack BR é uma coleção de utilidades como classes de documentos nacionais (CPF, CNPJ) com validação e classes de utilidade em geral. Desenvolvido para ser utilizado junto com o NetDevPack .NET DevPack BR é uma coleção de utilidades como classes de documentos nacionais (CPF, CNPJ) com validação e classes de utilidade em geral. Desenvolvido para ser utilizado junto com o NetDevPack - https://github.com/EduardoPires/NetDevPack + https://github.com/NetDevPack/Brasil NetDevPack.Brasil IconBR.png - https://github.com/EduardoPires/NetDevPack - EduardoPires - Public + README.md + NetDevPack;Brasil;CPF;CNPJ;CEP;TituloEleitor;validacao + https://github.com/NetDevPack/Brasil + git MIT - 1.0.4 - + @@ -26,13 +26,17 @@ True + + True + \ + - + <_CustomFiles Include="../../.github/hooks/commit-msg" /> - + diff --git a/tests/NetDevPack.Brasil.Tests/Documentos/CnpjValidadorTest.cs b/tests/NetDevPack.Brasil.Tests/Documentos/CnpjValidadorTest.cs index 128d818..4e78d39 100644 --- a/tests/NetDevPack.Brasil.Tests/Documentos/CnpjValidadorTest.cs +++ b/tests/NetDevPack.Brasil.Tests/Documentos/CnpjValidadorTest.cs @@ -1,5 +1,5 @@ -using System; -using FluentAssertions; +using System; +using AwesomeAssertions; using NetDevPack.Brasil.Documentos; using NetDevPack.Brasil.Documentos.Validacao; using NetDevPack.Domain; diff --git a/tests/NetDevPack.Brasil.Tests/Documentos/CpfValidadorTest.cs b/tests/NetDevPack.Brasil.Tests/Documentos/CpfValidadorTest.cs index 222a8e4..6345ed4 100644 --- a/tests/NetDevPack.Brasil.Tests/Documentos/CpfValidadorTest.cs +++ b/tests/NetDevPack.Brasil.Tests/Documentos/CpfValidadorTest.cs @@ -1,5 +1,5 @@ -using System; -using FluentAssertions; +using System; +using AwesomeAssertions; using NetDevPack.Brasil.Documentos; using NetDevPack.Brasil.Documentos.Validacao; using NetDevPack.Domain; diff --git a/tests/NetDevPack.Brasil.Tests/Documentos/TituloEleitorValidadorTest.cs b/tests/NetDevPack.Brasil.Tests/Documentos/TituloEleitorValidadorTest.cs index 6eae0e6..fa9e58e 100644 --- a/tests/NetDevPack.Brasil.Tests/Documentos/TituloEleitorValidadorTest.cs +++ b/tests/NetDevPack.Brasil.Tests/Documentos/TituloEleitorValidadorTest.cs @@ -1,5 +1,5 @@ -using System; -using FluentAssertions; +using System; +using AwesomeAssertions; using NetDevPack.Brasil.Documentos; using NetDevPack.Brasil.Documentos.Validacao; using NetDevPack.Domain; diff --git a/tests/NetDevPack.Brasil.Tests/Localizacao/CepTests.cs b/tests/NetDevPack.Brasil.Tests/Localizacao/CepTests.cs index c869737..d6a4edf 100644 --- a/tests/NetDevPack.Brasil.Tests/Localizacao/CepTests.cs +++ b/tests/NetDevPack.Brasil.Tests/Localizacao/CepTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using AwesomeAssertions; using NetDevPack.Brasil.Localizacao; using Xunit; @@ -32,5 +32,19 @@ public void Should_Not_Validate_Ceps(string codigo) var cep = new Cep(codigo); cep.EstaValido().Should().BeFalse(); } + + [Theory] + [InlineData("01310-100")] + [InlineData("01310100")] + public void Should_Normalize_Masked_And_Unmasked_Ceps(string codigo) + { + var cep = new Cep(codigo); + + cep.SemMascara().Should().Be("01310100"); + cep.ComMascara().Should().Be("01310-100"); + cep.Prefixo.Should().Be("01310"); + cep.Sufixo.Should().Be("100"); + cep.Equals(new Cep("01310100")).Should().BeTrue(); + } } } diff --git a/tests/NetDevPack.Brasil.Tests/NetDevPack.Brasil.Tests.csproj b/tests/NetDevPack.Brasil.Tests/NetDevPack.Brasil.Tests.csproj index 6d0ec6e..52c4d7a 100644 --- a/tests/NetDevPack.Brasil.Tests/NetDevPack.Brasil.Tests.csproj +++ b/tests/NetDevPack.Brasil.Tests/NetDevPack.Brasil.Tests.csproj @@ -1,18 +1,15 @@  - net8.0;net9.0;net10.0 + net10.0 + Exe false + true - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - + + diff --git a/tests/NetDevPack.Brasil.Tests/ReadmeExamplesTests.cs b/tests/NetDevPack.Brasil.Tests/ReadmeExamplesTests.cs new file mode 100644 index 0000000..0a92a43 --- /dev/null +++ b/tests/NetDevPack.Brasil.Tests/ReadmeExamplesTests.cs @@ -0,0 +1,32 @@ +using AwesomeAssertions; +using NetDevPack.Brasil.Documentos; +using NetDevPack.Brasil.Localizacao; +using Xunit; + +namespace NetDevPack.Brasil.Tests +{ + /// + /// Keeps the examples in README.md honest. + /// + public class ReadmeExamplesTests + { + [Fact(DisplayName = "README examples")] + [Trait("Category", "Readme")] + public void ReadmeExamples_ShouldWorkAsDocumented() + { + var cpf = new Cpf("915.212.540-87"); + cpf.SemMascara().Should().Be("91521254087"); + cpf.ComMascara().Should().Be("915.212.540-87"); + + var cnpj = new Cnpj("11.444.777/0001-61"); + cnpj.ComMascara().Should().Be("11.444.777/0001-61"); + + var titulo = new TituloEleitor("1023 8501 0671"); + titulo.EstaValido().Should().BeTrue(); + + var cep = new Cep("01310-100"); + cep.EstaValido().Should().BeTrue(); + cep.ComMascara().Should().Be("01310-100"); + } + } +}