首页 > 解决方案 > 如何保证编译和测试配置的库版本相同

问题描述

我有一个中型sbt项目(~20 个子项目,~50 个直接依赖项,~400 个传递依赖项)。我正在准备项目以启用单元测试 [0]。我遇到了一些奇怪的问题,并注意到用于某些依赖项的确切版本在CompileTest目标之间有所不同,即单元测试(sbt test)使用不同版本的库而不是直接使用项目(sbt run)运行。

sbt Compile/dependencyList我在[1]的输出之间产生了差异,sbt Test/dependencyList并看到了大约 50 个不同版本的依赖项。我知道我可以使用这些dependencyOverride设置,但是对于数百个依赖项手动执行似乎不切实际。每次我们想更新我们的直接依赖项时,我还必须更新该列表。

处理这种情况的预期方法是什么?即,如何确保在单元测试期间使用与在生产中运行时相同版本的依赖项?

[0]:迟到总比不到好!:)

[1]:dependencyList是来自sbt-dependency-graph的命令。

标签: unit-testingdependenciessbtdependency-management

解决方案


这是我的脚本,用于检测仅存在于生产配置中但不在测试配置中的依赖项(及其版本)。

它依赖于安装依赖图 SBT 插件

如果检测到任何仅生产依赖项,我将其集成到我们的 CI 并使其构建失败。

#!/bin/bash                                                                                                                                                                                             

# http://redsymbol.net/articles/unofficial-bash-strict-mode/                                                                                                                                            
set -euo pipefail
IFS=$'\n\t'

echo Getting dependencyList for production config
sbt "-Dsbt.log.noformat=true" dependencyList > dependency-list-production-raw.txt

echo Getting dependencyList for test config
sbt "-Dsbt.log.noformat=true" test:dependencyList > dependency-list-test-raw.txt

clean_up_dependency_list () {
  # non GNU sed version (less readable IMHO)                                                                                                                                                            
  # sed -n 's/[[:space:]]*\[info\][[:space:]]*\([^:]\{1,\}:[^:]\{1,\}:[^:]\{1,\}\)\s*$/\1/p' | sort | uniq                                                                                              
  sed --regexp-extended -n 's/^\s*\[info\]\s*([^:]+:[^:]+:[^:]+)\s*$/\1/p' | sort | uniq
}

clean_up_dependency_list < dependency-list-production-raw.txt > dependency-list-production.txt

clean_up_dependency_list < dependency-list-test-raw.txt > dependency-list-test.txt


echo Dependencies in dependency-list-production.txt: "$(wc -l < dependency-list-production.txt)"
echo Dependencies in dependency-list-test.txt: "$(wc -l < dependency-list-test.txt)"

dependencies_only_in_prod=$(comm -23 dependency-list-production.txt dependency-list-test.txt)

if [ -n "$dependencies_only_in_prod" ]; then
    cat <<EOF                                                                                                                                                                                           
                                                                                                                                                                                                        
    ERROR: Dependencies that are _not present in test_                                                                                                                                                  
    configuration detected!                                                                                                                                                                             
                                                                                                                                                                                                        
    This may signal a problem where a _different_ (older?) version of                                                                                                                                   
    a dependency will be used in production than what would be used in                                                                                                                                  
    production.                                                                                                                                                                                         
                                                                                                                                                                                                        
    Offending dependencies that are present  _only in prod_:                                                                                                                                            
                                                                                                                                                                                                        
$dependencies_only_in_prod                                                                                                                                                                              
                                                                                                                                                                                                        
    Full list of dependencies in test:                                                                                                                                                                  
                                                                                                                                                                                                        
$(cat dependency-list-test.txt)                                                                                                                                                                         
                                                                                                                                                                                                        
    List of dependencies in production:                                                                                                                                                                 
                                                                                                                                                                                                        
$(cat dependency-list-production.txt)                                                                                                                                                                   
                                                                                                                                                                                                        
EOF                                                                                                                                                                                                     

    exit 1
fi

推荐阅读