Skip to content

Commit

Permalink
fix crlf on neo.UnitTests and set all files to not use BOM (byte orde…
Browse files Browse the repository at this point in the history
…r mark) (#1057)

* Check dotnet format in travis

* Try to find the path

* Update .travis.yml

* Update .travis.yml

* Update .travis.yml

* Format travis

* test and source

* test and source

* Move to before the script

* testing if CRLF format is respected

* fix crlf on unit tests

* testing for crlf on unit tests too

* all utf-8

* neo/ on utf8 too

* looks like sudo is needed by travis

* download dos2unix 7.4.0

* temporaly remove | test

* back to | test

* explicit test zero

* no bom

* more non-boom to boom

* ensuring no_bom at all files

* Printing files with bom prefix

* Check both folders in one command

* Add verbosity

* Create .editorconfig

* Ensure the path of .editorconfig file

* Update .editorconfig

* Update .editorconfig

* Update .editorconfig

* use only dotnet-format

Is confirmed that .editorconfig works, so we should configure only this file

* basic formatting (just 5 files affected)

* dotnet version

* new lines on end

* update travis

* advice on auto crlf

* Update .travis.yml

* Update .travis.yml

* enforcing lf

* Fix dotnet format version

* removed dos2unix
  • Loading branch information
igormcoelho authored and shargon committed Aug 27, 2019
1 parent 25666ba commit 5995f32
Show file tree
Hide file tree
Showing 95 changed files with 390 additions and 366 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
###############################
# Core EditorConfig Options #
###############################

# dotnet-format requires version 3.1.37601
# dotnet tool update -g dotnet-format
# remember to have: git config --global core.autocrlf false #(which is usually default)

root = true

# Every file

[*]
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
end_of_line = lf
#indent_style = tab # TODO
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ mono: none
dotnet: 2.2.300

env:
- TEST_SUITE="without-cultures"
- TEST_SUITE="cultures"
- TEST_SUITE="without-cultures"
- TEST_SUITE="cultures"

before_install:
- cd neo.UnitTests
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ulimit -n 2048; fi
install:
- dotnet tool install -g dotnet-format --version 4.0.40103 --add-source https://dotnet.myget.org/F/format/api/v3/index.json
- export PATH="$PATH:$HOME/.dotnet/tools"
- dotnet-format --version
before_script:
- echo "Checking format..."
- dotnet format --check --dry-run -w . -v diagnostic # check C# formatting for neo.sln
- cd neo.UnitTests
script: |
dotnet restore
if [[ "$TEST_SUITE" == cultures ]]; then
dotnet restore
dotnet test
else
dotnet restore
find * -name *.csproj | xargs -I % dotnet add % package coverlet.msbuild
dotnet test -v n --filter FullyQualifiedName!=Neo.UnitTests.UT_Culture.All_Tests_Cultures /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
fi
Expand Down
2 changes: 1 addition & 1 deletion NuGet.Config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
Expand Down
306 changes: 153 additions & 153 deletions neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
@@ -1,134 +1,134 @@
using Akka.TestKit.Xunit2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.Consensus;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
using System.Linq;

namespace Neo.UnitTests.Consensus
{

[TestClass]
public class UT_ConsensusContext : TestKit
{
ConsensusContext _context;
KeyPair[] _validatorKeys;

[TestInitialize]
public void TestSetup()
{
TestBlockchain.InitializeMockNeoSystem();

var rand = new Random();
var mockWallet = new Mock<Wallet>();
mockWallet.Setup(p => p.GetAccount(It.IsAny<UInt160>())).Returns<UInt160>(p => new TestWalletAccount(p));

// Create dummy validators

_validatorKeys = new KeyPair[7];
for (int x = 0; x < _validatorKeys.Length; x++)
{
var pk = new byte[32];
rand.NextBytes(pk);

_validatorKeys[x] = new KeyPair(pk);
}

_context = new ConsensusContext(mockWallet.Object, TestBlockchain.GetStore())
{
Validators = _validatorKeys.Select(u => u.PublicKey).ToArray()
};
_context.Reset(0);
}

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestMethod]
public void TestMaxBlockSize_Good()
{
// Only one tx, is included

var tx1 = CreateTransactionWithSize(200);
_context.EnsureMaxBlockSize(new Transaction[] { tx1 });
EnsureContext(_context, tx1);

// All txs included

var max = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(_context.Snapshot);
var txs = new Transaction[max];

for (int x = 0; x < max; x++) txs[x] = CreateTransactionWithSize(100);

_context.EnsureMaxBlockSize(txs);
EnsureContext(_context, txs);
}

[TestMethod]
public void TestMaxBlockSize_Exceed()
{
// Two tx, the last one exceed the size rule, only the first will be included

var tx1 = CreateTransactionWithSize(200);
var tx2 = CreateTransactionWithSize(256 * 1024);
_context.EnsureMaxBlockSize(new Transaction[] { tx1, tx2 });
EnsureContext(_context, tx1);

// Exceed txs number, just MaxTransactionsPerBlock included

var max = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(_context.Snapshot);
var txs = new Transaction[max + 1];

for (int x = 0; x < max; x++) txs[x] = CreateTransactionWithSize(100);

_context.EnsureMaxBlockSize(txs);
EnsureContext(_context, txs.Take(max).ToArray());
}

private Transaction CreateTransactionWithSize(int v)
{
var r = new Random();
var tx = new Transaction()
{
Cosigners = new Cosigner[0],
Attributes = new TransactionAttribute[0],
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[0],
Sender = UInt160.Zero,
SystemFee = 0,
ValidUntilBlock = (uint)r.Next(),
Version = 0,
Witnesses = new Witness[0],
};

// Could be higher (few bytes) if varSize grows
tx.Script = new byte[v - tx.Size];
return tx;
}

private Block SignBlock(ConsensusContext context)
{
context.Block.MerkleRoot = null;

for (int x = 0; x < _validatorKeys.Length; x++)
{
_context.MyIndex = x;

var com = _context.MakeCommit();
_context.CommitPayloads[_context.MyIndex] = com;
}

// Manual block sign

using Akka.TestKit.Xunit2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.Consensus;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
using System.Linq;

namespace Neo.UnitTests.Consensus
{

[TestClass]
public class UT_ConsensusContext : TestKit
{
ConsensusContext _context;
KeyPair[] _validatorKeys;

[TestInitialize]
public void TestSetup()
{
TestBlockchain.InitializeMockNeoSystem();

var rand = new Random();
var mockWallet = new Mock<Wallet>();
mockWallet.Setup(p => p.GetAccount(It.IsAny<UInt160>())).Returns<UInt160>(p => new TestWalletAccount(p));

// Create dummy validators

_validatorKeys = new KeyPair[7];
for (int x = 0; x < _validatorKeys.Length; x++)
{
var pk = new byte[32];
rand.NextBytes(pk);

_validatorKeys[x] = new KeyPair(pk);
}

_context = new ConsensusContext(mockWallet.Object, TestBlockchain.GetStore())
{
Validators = _validatorKeys.Select(u => u.PublicKey).ToArray()
};
_context.Reset(0);
}

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestMethod]
public void TestMaxBlockSize_Good()
{
// Only one tx, is included

var tx1 = CreateTransactionWithSize(200);
_context.EnsureMaxBlockSize(new Transaction[] { tx1 });
EnsureContext(_context, tx1);

// All txs included

var max = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(_context.Snapshot);
var txs = new Transaction[max];

for (int x = 0; x < max; x++) txs[x] = CreateTransactionWithSize(100);

_context.EnsureMaxBlockSize(txs);
EnsureContext(_context, txs);
}

[TestMethod]
public void TestMaxBlockSize_Exceed()
{
// Two tx, the last one exceed the size rule, only the first will be included

var tx1 = CreateTransactionWithSize(200);
var tx2 = CreateTransactionWithSize(256 * 1024);
_context.EnsureMaxBlockSize(new Transaction[] { tx1, tx2 });
EnsureContext(_context, tx1);

// Exceed txs number, just MaxTransactionsPerBlock included

var max = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(_context.Snapshot);
var txs = new Transaction[max + 1];

for (int x = 0; x < max; x++) txs[x] = CreateTransactionWithSize(100);

_context.EnsureMaxBlockSize(txs);
EnsureContext(_context, txs.Take(max).ToArray());
}

private Transaction CreateTransactionWithSize(int v)
{
var r = new Random();
var tx = new Transaction()
{
Cosigners = new Cosigner[0],
Attributes = new TransactionAttribute[0],
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[0],
Sender = UInt160.Zero,
SystemFee = 0,
ValidUntilBlock = (uint)r.Next(),
Version = 0,
Witnesses = new Witness[0],
};

// Could be higher (few bytes) if varSize grows
tx.Script = new byte[v - tx.Size];
return tx;
}

private Block SignBlock(ConsensusContext context)
{
context.Block.MerkleRoot = null;

for (int x = 0; x < _validatorKeys.Length; x++)
{
_context.MyIndex = x;

var com = _context.MakeCommit();
_context.CommitPayloads[_context.MyIndex] = com;
}

// Manual block sign

Contract contract = Contract.CreateMultiSigContract(context.M, context.Validators);
ContractParametersContext sc = new ContractParametersContext(context.Block);
for (int i = 0, j = 0; i < context.Validators.Length && j < context.M; i++)
Expand All @@ -139,25 +139,25 @@ private Block SignBlock(ConsensusContext context)
}
context.Block.Witness = sc.GetWitnesses()[0];
context.Block.Transactions = context.TransactionHashes.Select(p => context.Transactions[p]).ToArray();
return context.Block;
}

private void EnsureContext(ConsensusContext context, params Transaction[] expected)
{
// Check all tx

Assert.AreEqual(expected.Length, context.Transactions.Count);
Assert.IsTrue(expected.All(tx => context.Transactions.ContainsKey(tx.Hash)));

Assert.AreEqual(expected.Length, context.TransactionHashes.Length);
Assert.IsTrue(expected.All(tx => context.TransactionHashes.Count(t => t == tx.Hash) == 1));

// Ensure length

var block = SignBlock(context);

Assert.AreEqual(context.GetExpectedBlockSize(), block.Size);
Assert.IsTrue(block.Size < NativeContract.Policy.GetMaxBlockSize(context.Snapshot));
}
}
}
return context.Block;
}

private void EnsureContext(ConsensusContext context, params Transaction[] expected)
{
// Check all tx

Assert.AreEqual(expected.Length, context.Transactions.Count);
Assert.IsTrue(expected.All(tx => context.Transactions.ContainsKey(tx.Hash)));

Assert.AreEqual(expected.Length, context.TransactionHashes.Length);
Assert.IsTrue(expected.All(tx => context.TransactionHashes.Count(t => t == tx.Hash) == 1));

// Ensure length

var block = SignBlock(context);

Assert.AreEqual(context.GetExpectedBlockSize(), block.Size);
Assert.IsTrue(block.Size < NativeContract.Policy.GetMaxBlockSize(context.Snapshot));
}
}
}
2 changes: 1 addition & 1 deletion neo.UnitTests/Consensus/UT_ConsensusServiceMailbox.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Akka.TestKit;
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/Cryptography/ECC/UT_ECDsa.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Wallets;
using System;
Expand Down Expand Up @@ -52,4 +52,4 @@ public void TestVerifySignature()
sa.VerifySignature(message, new BigInteger(-100), result[1]).Should().BeFalse();
}
}
}
}
2 changes: 1 addition & 1 deletion neo.UnitTests/Cryptography/ECC/UT_ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ public void TestToByteArray()
new ECFieldElement(BigInteger.Pow(new BigInteger(10), 77), ECCurve.Secp256k1).ToByteArray().Should().BeEquivalentTo(result3);
}
}
}
}
Loading

0 comments on commit 5995f32

Please sign in to comment.