<commit_before>local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
local meta = require "kong.meta"

describe("rockspec/meta", function()
  local rock, lua_srcs = {}
  local rock_filename

  lazy_setup(function()
    lua_srcs = pl_dir.getallfiles("./kong", "*.lua")
    assert.True(#lua_srcs > 0)

    local res = pl_dir.getfiles(".", "*.rockspec")
    assert(#res == 1, "more than 1 rockspec file")

    rock_filename = res[1]

    local f = assert(loadfile(res[1]))
    setfenv(f, rock)

    f()
  end)

  describe("meta", function()
    it("has a _NAME field", function()
      assert.is_string(meta._NAME)
    end)

    it("has a _VERSION field", function()
      assert.is_string(meta._VERSION)
      assert.matches("%d+%.%d+%.%d+", meta._VERSION)
    end)

    it("has a _VERSION_TABLE field", function()
      assert.is_table(meta._VERSION_TABLE)
      assert.is_number(meta._VERSION_TABLE.major)
      assert.is_number(meta._VERSION_TABLE.minor)
      assert.is_number(meta._VERSION_TABLE.patch)
      -- suffix optional
    end)

    it("has a _SERVER_TOKENS field", function()
      assert.is_string(meta._SERVER_TOKENS)
    end)

    it("has a _SERVER_TOKENS field that equals to _NAME/_VERSION", function()
      assert.equal(meta._NAME .. "/" .. meta._VERSION, meta._SERVER_TOKENS)
    end)

    it("has a _DEPENDENCIES field", function()
      assert.is_table(meta._DEPENDENCIES)
      assert.is_table(meta._DEPENDENCIES.nginx)
    end)
  end)

  it("has same version as meta", function()
    assert.matches(meta._VERSION, rock.version:match("(.-)%-.*$"))
  end)

  it("has same name as meta", function()
    assert.equal(meta._NAME, rock.package)
  end)

  it("has correct version in filename", function()
    local pattern = meta._VERSION:gsub("%.", "%%."):gsub("-", "%%-")
    assert.matches(pattern, rock_filename)
  end)

  describe("modules", function()

    it("all modules named as their path", function()
      for mod_name, mod_path in pairs(rock.build.modules) do
        if mod_name ~= "kong" then
          mod_path = mod_path:gsub("%.lua", ""):gsub("/", '.'):gsub("%.init", "")
          assert(mod_name == mod_path,
                 mod_path .. " has different name (" .. mod_name .. ")")
        end
      end
    end)

    it("all rockspec files do exist", function()
      for mod_name, mod_path in pairs(rock.build.modules) do
        assert(pl_path.exists(mod_path),
               mod_path .. " does not exist (" .. mod_name .. ")")
      end
    end)
  end)

  describe("requires", function()
    it("requires in the codebase are defined modules in the rockspec", function()
      for _, src in ipairs(lua_srcs) do
        local str = pl_utils.readfile(src)

        for _, mod in string.gmatch(str, "require%s*([\"'])(kong%..-)%1") do<commit_msg>tests(rockspec) match require with parentheses<commit_after>local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
local meta = require "kong.meta"

describe("rockspec/meta", function()
  local rock, lua_srcs = {}
  local rock_filename

  lazy_setup(function()
    lua_srcs = pl_dir.getallfiles("./kong", "*.lua")
    assert.True(#lua_srcs > 0)

    local res = pl_dir.getfiles(".", "*.rockspec")
    assert(#res == 1, "more than 1 rockspec file")

    rock_filename = res[1]

    local f = assert(loadfile(res[1]))
    setfenv(f, rock)

    f()
  end)

  describe("meta", function()
    it("has a _NAME field", function()
      assert.is_string(meta._NAME)
    end)

    it("has a _VERSION field", function()
      assert.is_string(meta._VERSION)
      assert.matches("%d+%.%d+%.%d+", meta._VERSION)
    end)

    it("has a _VERSION_TABLE field", function()
      assert.is_table(meta._VERSION_TABLE)
      assert.is_number(meta._VERSION_TABLE.major)
      assert.is_number(meta._VERSION_TABLE.minor)
      assert.is_number(meta._VERSION_TABLE.patch)
      -- suffix optional
    end)

    it("has a _SERVER_TOKENS field", function()
      assert.is_string(meta._SERVER_TOKENS)
    end)

    it("has a _SERVER_TOKENS field that equals to _NAME/_VERSION", function()
      assert.equal(meta._NAME .. "/" .. meta._VERSION, meta._SERVER_TOKENS)
    end)

    it("has a _DEPENDENCIES field", function()
      assert.is_table(meta._DEPENDENCIES)
      assert.is_table(meta._DEPENDENCIES.nginx)
    end)
  end)

  it("has same version as meta", function()
    assert.matches(meta._VERSION, rock.version:match("(.-)%-.*$"))
  end)

  it("has same name as meta", function()
    assert.equal(meta._NAME, rock.package)
  end)

  it("has correct version in filename", function()
    local pattern = meta._VERSION:gsub("%.", "%%."):gsub("-", "%%-")
    assert.matches(pattern, rock_filename)
  end)

  describe("modules", function()

    it("all modules named as their path", function()
      for mod_name, mod_path in pairs(rock.build.modules) do
        if mod_name ~= "kong" then
          mod_path = mod_path:gsub("%.lua", ""):gsub("/", '.'):gsub("%.init", "")
          assert(mod_name == mod_path,
                 mod_path .. " has different name (" .. mod_name .. ")")
        end
      end
    end)

    it("all rockspec files do exist", function()
      for mod_name, mod_path in pairs(rock.build.modules) do
        assert(pl_path.exists(mod_path),
               mod_path .. " does not exist (" .. mod_name .. ")")
      end
    end)
  end)

  describe("requires", function()
    it("requires in the codebase are defined modules in the rockspec", function()
      for _, src in ipairs(lua_srcs) do
        local str = pl_utils.readfile(src)

        -- PCRE: require\s*\(?\s*(["''])(kong\.[\w_.-]+[\w_.-])(["''])
        for _, mod in string.gmatch(str, "require%s*%(?%s*([\"'])(kong%.[%w_.-]+[%w_-])%1") do
{'fraction_non_alphanumeric': 0.15825846579129232, 'fraction_numerical': 0.002073255010366275, 'mean_word_length': 2.9924137931034482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4724741', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1928, 'n_words': 445}