updater: improve redundant find logic and add ci for it

This commit is contained in:
Felix Yan 2022-12-05 20:43:31 +02:00
parent 15d2c90170
commit 840fc72b3c
No known key found for this signature in database
GPG Key ID: 786C63F330D7CB92
5 changed files with 68 additions and 29 deletions

12
.github/workflows/test.yaml vendored Normal file
View File

@ -0,0 +1,12 @@
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
bundler-cache: true
- name: Run tests
run: bundle exec sus

7
gems.rb Normal file
View File

@ -0,0 +1,7 @@
source "https://rubygems.org"
gem "colorize"
gem "concurrent-ruby"
gem "ipaddr"
gem "public_suffix"
gem "sus"

14
test/redundant.rb Normal file
View File

@ -0,0 +1,14 @@
require_relative '../verify'
let (:lines) { File.readlines("accelerated-domains.china.conf").filter { |line| !line.empty? } }
it "should find redundant domains" do
expect(CheckRedundant(lines, [], 'qq.com')).to be == false
expect(CheckRedundant(lines, [], 'www.qq.com')).to be == false
expect(CheckRedundant(lines, [], 'qq.cn')).to be == false
expect(CheckRedundant(lines, [], 'www.qq.cn')).to be == false
end
it "should add new domains" do
expect(CheckRedundant(lines, [], 'what.a.wonderful.domain')).to be == "server=/what.a.wonderful.domain/114.114.114.114\n"
end

View File

@ -2,6 +2,7 @@
require 'domain_name'
require 'optparse'
require 'ostruct'
require_relative 'verify'
options = OpenStruct.new
options.sort = true
@ -37,34 +38,11 @@ changed = false
options.add.each do |domain|
domain = DomainName.normalize(domain)
new_line = "server=/#{domain}/114.114.114.114\n"
disabled_line = "#server=/#{domain}/114.114.114.114"
if lines.include? new_line
puts "Domain already exists: #{domain}"
else
if disabled_lines.any? { |line| line.start_with? disabled_line }
puts "Domain already disabled: #{domain}"
else
# Check for duplicates
test_domain = domain
while test_domain.include? '.'
test_domain = test_domain.partition('.').last
_new_line = "server=/#{test_domain}/114.114.114.114\n"
_disabled_line = "#server=/#{test_domain}/114.114.114.114"
if lines.include? _new_line
puts "Redundant domain already exists: #{test_domain}"
break
elsif disabled_lines.any? { |line| line.start_with? _disabled_line }
puts "Redundant domain already disabled: #{test_domain}"
break
end
end
next if test_domain.include? '.'
puts "New domain added: #{domain}"
lines << new_line
changed = true
end
new_line = CheckRedundant(lines, disabled_lines, domain)
if new_line != false
puts "New domain added: #{domain}"
lines << new_line
changed = true
end
end

View File

@ -6,7 +6,6 @@ require 'ipaddr'
require 'public_suffix'
require 'resolv'
class ChinaListVerify
def initialize(
dns=nil,
@ -224,6 +223,35 @@ class ChinaListVerify
end
end
# Operates on the raw file to preserve commented out lines
def CheckRedundant(lines, disabled_lines, domain)
new_line = "server=/#{domain}/114.114.114.114\n"
disabled_line = "#server=/#{domain}/114.114.114.114"
if lines.include? new_line
puts "Domain already exists: #{domain}"
return false
elsif disabled_lines.any? { |line| line.start_with? disabled_line }
puts "Domain already disabled: #{domain}"
return false
else
# Check for duplicates
test_domain = domain
while test_domain.include? '.'
test_domain = test_domain.partition('.').last
_new_line = "server=/#{test_domain}/114.114.114.114\n"
_disabled_line = "#server=/#{test_domain}/114.114.114.114"
if lines.include? _new_line
puts "Redundant domain already exists: #{test_domain}"
return false
elsif disabled_lines.any? { |line| line.start_with? _disabled_line }
puts "Redundant domain already disabled: #{test_domain}"
return false
end
end
end
return new_line
end
if __FILE__ == $0
require 'optparse'
require 'ostruct'