<commit_before>const Config = require("truffle-config");
const Web3 = require("web3");
const TruffleResolver = require("truffle-resolver");
const assert = require("assert");
const Ganache = require("ganache-core");
const ENS = require("../ens");
const path = require("path");
let ganacheOptions,
  config,
  options,
  server,
  ens,
  fromAddress,
  provider,
  workingDirectory,
  contractsBuildDirectory;

describe("ENS class", () => {
  beforeEach(done => {
    ganacheOptions = {
      mnemonic:
        "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat",
      total_accounts: 10,
      default_ether_balance: 100
    };
    workingDirectory = path.join(__dirname, "sources", "init");
    contractsBuildDirectory = path.join(workingDirectory, "build");
    config = Config.detect({
      working_directory: workingDirectory,
      contracts_build_directory: contractsBuildDirectory
    });
    server = Ganache.server(ganacheOptions);
    server.listen(8545, done);
    provider = new Web3.providers.HttpProvider("http://localhost:8545");
    options = {
      provider,
      resolver: new TruffleResolver(config)
    };
    ens = new ENS(options);
    fromAddress = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57";
  });
  afterEach(done => {
    if (server) {
      server.close(() => {
        server = null;
        done();
      });
    }
  });

  describe("deployNewENSRegistry", () => {
    it("deploys a new registry and returns valid addresses", async () => {
      let {
        registryOwnerAddress,
        registryAddress
      } = await ens.deployNewENSRegistry(fromAddress);
      assert(Web3.utils.isAddress(registryOwnerAddress));
      assert(Web3.utils.isAddress(registryAddress));
    });
    it("returns addresses that are not 0x0", async () => {
      let {
        registryOwnerAddress,
        registryAddress
      } = await ens.deployNewENSRegistry(fromAddress);
      assert(registryAddress !== "0x0000000000000000000000000000000000000000");
      assert(
        registryOwnerAddress !== "0x0000000000000000000000000000000000000000"
      );
    });
  });
});
<commit_msg>Write a couple of tests for the ens utility<commit_after>const Config = require("truffle-config");
const Web3 = require("web3");
const TruffleResolver = require("truffle-resolver");
const assert = require("assert");
const Ganache = require("ganache-core");
const ENS = require("../ens");
const path = require("path");
const sinon = require("sinon");
const HDWalletProvider = require("truffle-hdwallet-provider");
let ganacheOptions,
  config,
  options,
  server,
  ens,
  fromAddress,
  provider,
  workingDirectory,
  contractsBuildDirectory,
  addressToSet,
  registryArtifact;

describe("ENS class", () => {
  before(() => {
    ganacheOptions = {
      mnemonic:
        "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat",
      total_accounts: 1,
      default_ether_balance: 100
    };
    server = Ganache.server(ganacheOptions);
    server.listen(8545, () => {});
  });
  after(done => {
    if (server) {
      server.close(() => {
        server = null;
        done();
      });
    }
  });
  beforeEach(() => {
    workingDirectory = path.join(__dirname, "sources", "init");
    contractsBuildDirectory = path.join(workingDirectory, "build", "contracts");
    config = Config.detect({
      working_directory: workingDirectory,
      contracts_build_directory: contractsBuildDirectory
    });
    provider = new HDWalletProvider(
      ganacheOptions.mnemonic,
      "http://localhost:8545"
    );
    options = {
      provider,
      resolver: new TruffleResolver(config)
    };
    registryArtifact = require(path.join(
      contractsBuildDirectory,
      "ENSRegistry.json"
    ));
    sinon
      .stub(options.resolver.sources[3], "require")
      .withArgs("@ensdomains/ens/ENSRegistry")
      .returns(registryArtifact);
    ens = new ENS(options);
    fromAddress = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57";
  });
  afterEach(() => {
    options.resolver.sources[3].require.restore();
  });

  describe("deployNewENSRegistry", () => {
    it("deploys a new registry and returns valid addresses", async () => {
      let {
        registryOwnerAddress,
        registryAddress
      } = await ens.deployNewENSRegistry(fromAddress);
      assert(Web3.utils.isAddress(registryOwnerAddress));
      assert(Web3.utils.isAddress(registryAddress));
    });
    it("deploys a new registry and returns non 0x0 addresses", async () => {
      let {
        registryOwnerAddress,
        registryAddress
      } = await ens.deployNewENSRegistry(fromAddress);
      assert(registryAddress !== "0x0000000000000000000000000000000000000000");
      assert(
        registryOwnerAddress !== "0x0000000000000000000000000000000000000000"
      );
    });
  });

  describe("register(address, name, from)", () => {
    describe("when there is no registry deployed", () => {
      beforeEach(() => {
        sinon.spy(ens, "deployNewENSRegistry");
        sinon.stub();
        addressToSet = "0x1234567890123456789012345678901234567890";
      });
      afterEach(() => {
        ens.deployNewENSRegistry.restore();
      });

      it("calls deployNewENSRegistry", async () => {
        try {
          await ens.register(addressToSet, "namezzz", fromAddress);
        } catch (error) {
          const expectedMessageSnippet = `The default address or address provided in the "from"`;
          if (error.message.includes(expectedMessageSnippet)) {
            assert(ens.deployNewENSRegistry.called);
          } else {
            throw error;
          }
        }
      });
    });
    // describe("when there is a registry deployed", () => {
    //   it("registers a name and sets the resolver", () => {
    //
    //   });
    // });
  });
});

{'fraction_non_alphanumeric': 0.13155631986242478, 'fraction_numerical': 0.05141874462596733, 'mean_word_length': 3.098661028893587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '509191', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1658, 'n_words': 336}